yodawg
yodawg

Reputation: 41

python extract id value from href source

I've managed to extract the href URI's using beautifulsoup from the source of the page, however I now want to extract the UID value from multiple instances of the example below:

e.g

<a href="test.html?uid=5444974">
<a href="test.html?uid=5444972">
<a href="test.html?uid=54444972">

Help would be greatly appreciated!

Upvotes: 2

Views: 1030

Answers (2)

Jon Clements
Jon Clements

Reputation: 142146

Use urlparse and parse_qs:

html = """<a href="test.html?uid=5444974">
<a href="test.html?uid=5444972">
<a href="test.html?uid=54444972">
"""

from bs4 import BeautifulSoup as BS
from urlparse import urlparse, parse_qs
soup = BS(html)
for a in soup('a', href=True):
    print parse_qs(urlparse(a['href']).query)['uid'][0]

Output:

5444974
5444972
54444972

Upvotes: 1

zhangyangyu
zhangyangyu

Reputation: 8610

>>> html
'<a href="test.html?uid=5444974">\n<a href="test.html?uid=5444972">\n<a href="test.html?uid=54444972">'
>>> soup = BeautifulSoup(html)
>>> ass = soup.find_all('a')
>>> r = re.compile('uid=(\d+)')
>>> uids = []
>>> for a in ass:
...     uids.append(r.search(a['href']).group(1))
... 
>>> uids
['5444974', '5444972', '54444972']
>>> 

Upvotes: 1

Related Questions