find() and rfind() questions

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 8
Reputation: betatype is an unknown quantity at this point 
Solved Threads: 0
betatype betatype is offline Offline
Newbie Poster

find() and rfind() questions

 
0
  #1
Jan 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: find() and rfind() questions

 
0
  #2
Jan 4th, 2009
Supply a starting index for str.find() and update it each iteration. Example:
  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:
  1. >>> ['User 1', 'User 2', 'User 3', 'User 4', 'User 5']
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: betatype is an unknown quantity at this point 
Solved Threads: 0
betatype betatype is offline Offline
Newbie Poster

Re: find() and rfind() questions

 
0
  #3
Jan 4th, 2009
Genius! Thanks so much.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: find() and rfind() questions

 
0
  #4
Jan 4th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC