我已经设法从页面源中使用 beautifulsoup 提取了 href URI,但是我现在想从以下示例的多个实例中提取 UID 值:
例如
<a href="test.html?uid=5444974">
<a href="test.html?uid=5444972">
<a href="test.html?uid=54444972">
帮助将不胜感激!
我已经设法从页面源中使用 beautifulsoup 提取了 href URI,但是我现在想从以下示例的多个实例中提取 UID 值:
例如
<a href="test.html?uid=5444974">
<a href="test.html?uid=5444972">
<a href="test.html?uid=54444972">
帮助将不胜感激!
>>> 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']
>>>
使用urlparse
和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]
输出:
5444974
5444972
54444972