from BeautifulSoup import BeautifulSoup
html = '''<a href="some_url">next</a>
<span class="class"><a href="another_url">later</a></span>'''
soup = BeautifulSoup(html)
for a in soup.find_all('a', href=True):
print "Found the URL:", a['href']
结果将是:
Found the URL: some_url
Found the URL: another_url
您可以通过以下方式使用
find_all
来查找具有href
属性的每个a
元素,并打印每个元素:结果将是:
请注意,如果您使用的是旧版本的BeautifulSoup(在版本4之前),则此方法的名称为
findAll
。在版本4中,美化组的方法名were changed to be PEP 8 compliant,因此应该改用find_all
。如果希望所有带有
href
的标记,可以省略name
参数:相关问题 更多 >
编程相关推荐