python从href源中提取id值

时间:2013-07-16 15:51:27

标签: python regex beautifulsoup

我已经设法从页面源使用beautifulsoup提取href URI,但是我现在想要从以下示例的多个实例中提取UID值:

e.g

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

非常感谢帮助!

2 个答案:

答案 0 :(得分:1)

>>> 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']
>>> 

答案 1 :(得分:1)

使用urlparseparse_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]

输出:

5444974
5444972
54444972