| | |
find() and rfind() questions
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 8
Reputation:
Solved Threads: 0
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.
[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.
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
Supply a starting index for str.find() and update it each iteration. Example:
Output:
Python Syntax (Toggle Plain Text)
users = '''Users <a>User 1</a> <a>User 2</a> <a>User 3</a> <a>User 4</a> <a>User 5</a> ''' userList = [] idx = 0 while True: linkstart=users.find("<a>",idx) linkend=users.find("</a>", idx) if -1 in [linkstart,linkend]: break else: userList.append(users[linkstart+3:linkend]) idx = linkend+4 print userList
Python Syntax (Toggle Plain Text)
>>> ['User 1', 'User 2', 'User 3', 'User 4', 'User 5']
•
•
Join Date: Nov 2007
Posts: 148
Reputation:
Solved Threads: 32
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
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
![]() |
Other Threads in the Python Forum
- Previous Thread: Sent a pack of data
- Next Thread: Class module help
| Thread Tools | Search this Thread |
accessdenied advanced application argv beginner change color command convert csv cursor def dictionary digital dynamic dynamically edit editing enter event examples excel file float format frange function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session simple skinning smtp sprite stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython





