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.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1200
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])
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1374
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests

cron