我正在尝试使用 BeautifulSoup 抓取网站,但遇到了问题。 我正在学习在 python 2.7 中完成的教程,它具有完全相同的代码并且没有任何问题。
import urllib.request
from bs4 import *
htmlfile = urllib.request.urlopen("http://en.wikipedia.org/wiki/Steve_Jobs")
htmltext = htmlfile.read()
soup = BeautifulSoup(htmltext)
title = (soup.title.text)
body = soup.find("Born").findNext('td')
print (body.text)
如果我尝试运行我得到的程序,
Traceback (most recent call last):
File "C:\Users\USER\Documents\Python Programs\World Population.py", line 13, in <module>
body = soup.find("Born").findNext('p')
AttributeError: 'NoneType' object has no attribute 'findNext'
这是 python 3 的问题还是我太天真了?
最佳答案
find
和 find_all
方法不搜索文档中的任意文本,它们搜索 HTML 标签。 文档明确说明了这一点(我的斜体):
传入名称的值,您将告诉 Beautiful Soup 只考虑具有特定名称的标签。文本字符串将被忽略,名称不匹配的标签也会被忽略。这是最简单的用法:
soup.find_all("title")
# [<title>The Dormouse's story</title>]
这就是为什么您的 soup.find("Born")
返回 None
以及为什么它提示 NoneType
(None
) 没有 findNext()
方法。
您引用的页面包含(在撰写此答案时)“born”一词的八个副本,其中没有一个是标签。
查看该页面的 HTML 源代码,您会发现最好的选择可能是寻找正确的跨度(格式化为可读性):
<th scope="row" style="text-align: left;">Born</th>
<td>
<span class="nickname">Steven Paul Jobs</span><br />
<span style="display: none;">(<span class="bday">1955-02-24</span>)</span>February 24, 1955<br />
</td>
关于Python: AttributeError: 'NoneType' 对象没有属性 'findNext',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21421417/