所以,我想从“h1”标签中得到一个文本。我使用的是BeutifulSoup,它可以正常工作,直到“article”标记中没有“h1”标记,然后我得到“'NoneType'对象没有属性'contents'错误。 代码如下:
from bs4 import BeautifulSoup
page =
"<article>
<a href="http://something">
</a> (missing "h1")
<a href="http://something">
</a>
</article>
<article>
<a href="http://something">
</a>
<a href="http://something">
<h1>something</h1>
</a>
</article>
<article>
<a href="http://something">
</a>
<a href="http://something">
<h1>something</h1>
</a>
</article>"
soup = BeautifulSoup(page, "lxml")
h1s = []
articles = soup.find_all("article")
for i in range(1,len(articles)):
h1s.append(articles[i].h1.contents)
这些是当我检查有h1标记和没有h1标记的行时的消息。在
^{pr2}$
您应该只需遍历
articles
,这是一个列表,然后使用find_all()
方法获取a
标记内的所有h1
,然后将其text
添加到h1s中相关问题 更多 >
编程相关推荐