943,696 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2165
  • Python RSS
Jan 4th, 2009
0

find() and rfind() questions

Expand Post »
First post so thanks in advance for any help. I am looking to pull the anchor text from a series of links in some html. I am doing this with find() and rfind():

[code]linkend=users.find("</a>:")
linkstart=users.rfind(">",0,linkend)[code]

My question is that once I have found the first link, how do I then continue to move on from that point? If I were to run this in a for loop 50 times it would just give me the first link 50 times, rather than finding all 50 links on the page, which is what I'm after. Thanks for your help everybody.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
betatype is offline Offline
8 posts
since Jan 2009
Jan 4th, 2009
0

Re: find() and rfind() questions

Supply a starting index for str.find() and update it each iteration. Example:
Python Syntax (Toggle Plain Text)
  1. users = '''Users
  2. <a>User 1</a>
  3. <a>User 2</a>
  4. <a>User 3</a>
  5. <a>User 4</a>
  6. <a>User 5</a>
  7. '''
  8.  
  9. userList = []
  10. idx = 0
  11. while True:
  12. linkstart=users.find("<a>",idx)
  13. linkend=users.find("</a>", idx)
  14. if -1 in [linkstart,linkend]:
  15. break
  16. else:
  17. userList.append(users[linkstart+3:linkend])
  18. idx = linkend+4
  19.  
  20. print userList
Output:
Python Syntax (Toggle Plain Text)
  1. >>> ['User 1', 'User 2', 'User 3', 'User 4', 'User 5']
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Jan 4th, 2009
0

Re: find() and rfind() questions

Genius! Thanks so much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
betatype is offline Offline
8 posts
since Jan 2009
Jan 4th, 2009
0

Re: find() and rfind() questions

Is this for a homework assignment? I just want to know if you're allowed to use any Python tool you want, or if you're limited to what you've covered in class.

I'm sure there is a way to do it with find, but why don't you check out this tutorial on HTMLParser. It includes an example of how to do what you're trying to do.
http://cis.poly.edu/cs912/parsing.txt
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Sent a pack of data
Next Thread in Python Forum Timeline: Class module help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC