我有一个正则表达式,在标签中搜索"href"属性,但它目前效果不佳:
<a[^>]* href="([^"]*)"
它从中发现:
<a href="http://something" title="Development of the Python language and website">Core Development</a>
这一行:
<a href="http://something"
但我只需要找到:
http://something
这似乎对我有用吗?您可以自己查看工作演示.
matches = re.findall(r'<a[^>]* href="([^"]*)"', html)
相反,我会用美丽的汤来实现这一目标......
from bs4 import BeautifulSoup
html = '''
<a href="http://something" title="Development of the Python language and website">Core Development</a>
<a href="http://something.com" title="Development of the Python language and website">Core Development</a>
'''
soup = BeautifulSoup(html)
for a in soup.find_all('a', href=True):
    print a['href']
注意:如果您使用的是旧版的Beautiful Soup,那么您将使用以下代码:
for a in soup.findAll('a', href=True):
| 归档时间: | 
 | 
| 查看次数: | 3975 次 | 
| 最近记录: |