我有以下这个soup
:
<a href="some_url">next</a>
<span class="class">...</span>
我想提取href属性,其值为"some_url"
如果只有一个标签,我可以做到,但这里有两个标签。我也可以获取文本'next'
,但那不是我想要的。
此外,是否有一个API的良好描述和示例。我正在使用标准文档,但我正在寻找更加组织化的内容。
我有以下这个soup
:
<a href="some_url">next</a>
<span class="class">...</span>
我想提取href属性,其值为"some_url"
如果只有一个标签,我可以做到,但这里有两个标签。我也可以获取文本'next'
,但那不是我想要的。
此外,是否有一个API的良好描述和示例。我正在使用标准文档,但我正在寻找更加组织化的内容。
find_all
方法找到每个带有href
属性的标签,并将它们打印出来:# Python2
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']
# The output would be:
# Found the URL: some_url
# Found the URL: another_url
# Python3
from bs4 import BeautifulSoup
html = '''<a href="https://some_url.com">next</a>
<span class="class">
<a href="https://some_other_url.com">another_url</a></span>'''
soup = BeautifulSoup(html)
for a in soup.find_all('a', href=True):
print("Found the URL:", a['href'])
# The output would be:
# Found the URL: https://some_url.com
# Found the URL: https://some_other_url.com
请注意,如果你使用的是较早版本的BeautifulSoup(4.0 之前的版本),则此方法的名称为findAll
。在版本 4 中,BeautifulSoup 的方法名称已更改以符合 PEP 8 标准,因此您应该改用 find_all
。
如果您想要获取所有具有href
属性的标签,可以省略 name
参数:
href_tags = soup.find_all(href=True)
javascript:void(0)
, / en / support / index.html
,#smp-navigationList
)? - voices<a href="some_url">NEXT</a>
- abdoulsn
soup.find('a')['href']
让我感到困惑的是,我使用 Django(HTML)来查看它,实际上在呈现之前会删除 href:soup.find('a') 只剩下 'next'。 - dkgirl