| | |
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
Views: 928 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt apache application argv beginner binary book calculator change cipher code command converter dictionaries dictionary dynamic event examples excel file float format ftp function google gui hints homework import inches input java keyboard launcher line linux list lists loop maze microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook plugin port prime print program programming projects py2exe pygame pyqt python random recursion recursive refresh remote scrolledtext session signal simple ssh string strings strip table terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 valueerror variable verify vigenere windows wordgame wxpython xlwt





