目前我正在分析其他人的代码,现在我正在弄清楚BeautifulSoup.hyperlinks超链接变量必须具有。有人知道这方面的文件吗?我在官方网站上没有发现任何东西。问题是,当我打印时,下面的代码给出“None”汤.超链接地址:
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="intermezzo">this is a link: http://www.link.nl/
<a href="http://www.link.nl" title="link title" target="link target" class="link class">link label</a>
</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc)
print soup.hyperlinks
我希望有人能帮我?你知道吗
BeautifulSoup对象没有
.hyperlinks
属性;也从来没有这样的东西。你知道吗相反,BeautifulSoup无法识别的任何属性访问都会变成对
.find()
的调用。soup.hyperlinks
被解释为soup.find('hyperlinks')
,搜索第一个<hyperlinks>
HTML元素。因为没有这样的标记,所以返回None
。你知道吗要查找HTML文档中的所有超链接,只需遍历所有
a
标记,仅限于具有href
属性的标记:演示:
您还可以获取所有
href
属性:相关问题 更多 >
编程相关推荐