Finding the value that comes after string.find

This is the place for queries that don't fit in any of the other categories.

Finding the value that comes after string.find

Postby CRITICVIP » Tue Aug 13, 2013 11:30 pm

I know the title doesn't make much sense, but I didnt know how to explain my problem.

Anywho, I've opened a page's source in URLLIB
starturlsource = starturlopen.read()
string.find(starturlsource, '<a href="/profile.php?id=')
And I used string.find to find a specific area in the page's source.
I want to store what comes after ?id= in a variable.
Can someone help me with this?
I dont want to do any html parsing.
CRITICVIP
 
Posts: 1
Joined: Tue Aug 13, 2013 11:21 pm

Re: Finding the value that comes after string.find

Postby micseydel » Wed Aug 14, 2013 12:26 am

Could you give some sample input and output? Like exactly what the string you're doing with looks like, and what you want to end up in the variable.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 2996
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Finding the value that comes after string.find

Postby metulburr » Wed Aug 14, 2013 1:54 am

well using str.find, this would be a way. I emphasize the "a" because when parsing html normally BeautifulSoup or lxml.html or some other variant would be better equipt to handle parsing html then str.find. But to give you an example:

Code: Select all
s = '''somethingelse<a href="/profile.php?id=20</a>somethingelse'''
search = '<a href="/profile.php?id='
start = s.find(search) + len(search)
end = s.find('</a>')
print(s[start:end])
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2228
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], roadrage, Yahoo [Bot] and 10 guests