Python BeautifulSoup - TypeError: 'NoneType' 对象不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26603435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BeautifulSoup - TypeError: 'NoneType' object is not callable
提问by Andrius
I need to make my code backwards compatible with python2.6 and BeautifulSoup 3. My code was written using python2.7 and at this case using BS4. But when I try to run it at squeezy server, I get this error (it has python2.6 and bs3):
我需要使我的代码向后兼容 python2.6 和 BeautifulSoup 3。我的代码是使用 python2.7 编写的,在这种情况下使用 BS4。但是当我尝试在挤压服务器上运行它时,我收到这个错误(它有 python2.6 和 bs3):
try:
from bs4 import BeautifulSoup
except ImportError:
from BeautifulSoup import BeautifulSoup
gmp = open(fname, 'r')
soup = BeautifulSoup(gmp)
p = soup.body.div.find_all('p')
p = soup.body.div.find_all('p')
TypeError: 'NoneType' object is not callable
If I change to:
如果我改为:
p = soup.body.div.findAll('p')
then I get this error:
然后我收到这个错误:
p = soup.body.div.findAll('p')
TypeError: 'NoneType' object is not callable
Update of thrown error
抛出错误的更新
File "/home/user/openerp/7.0/addons/my_module/models/gec.py", line 401, in parse_html_data
p = soup.body.div.findAll('p') #used findAll instead of find_all for backwards compatability to bs3 version
TypeError: 'NoneType' object is not callable
Either way, both approaches work on my Ubuntu with python2.7 and bs4, but not on squeezy. Is there any other difference between those versions that I don't see/know and gives me this error?
无论哪种方式,这两种方法都适用于我的 Ubuntu,使用 python2.7 和 bs4,但不适用于挤压。我看不到/不知道并给我这个错误的这些版本之间还有其他区别吗?
回答by Martijn Pieters
You are using BeautifulSoup 3, but are using BeautifulSoup 4 syntax.
您使用的是 BeautifulSoup 3,但使用的是 BeautifulSoup 4 语法。
Your fallback is at fault here:
您的后备在这里有问题:
try:
from bs4 import BeautifulSoup
except ImportError:
from BeautifulSoup import BeautifulSoup
If you want to use either version 3 or 4, stick to version 3 syntax:
如果您想使用第 3 版或第 4 版,请坚持使用第 3 版语法:
p = soup.body.div.findAll('p')
because find_allis not a valid method in BeautifulSoup 3, so it is instead interpreted as a tag search. There is no find_alltag in your HTML, so Noneis returned, which you then try to call.
因为find_all在 BeautifulSoup 3 中不是有效方法,所以它被解释为标签搜索。find_all您的 HTML 中没有标记,因此None返回,然后您尝试调用它。
Next, the parserused by BeautifulSoup 3 will respond differently to broken or incomplete HTML. If you have lxmlinstalled on Ubuntu, then that'll be used as the default parser, and it'll insert a missing <body>tag for you. BeautifulSoup 3 may leave that out.
接下来,BeautifulSoup 3 使用的解析器将对损坏或不完整的 HTML 做出不同的响应。如果您已经lxml安装在 Ubuntu 上,那么它将被用作默认解析器,它会<body>为您插入一个缺失的标签。BeautifulSoup 3 可能会忽略这一点。
I strongly urge you to instead removethe fallback, and stick with BeautifulSoup version 4 only. Version 3 has been discontinued years ago, and contains unfixed bugs. BeautifulSoup 4 also offers additional features you may want to make use of.
我强烈建议您删除回退,并仅坚持使用 BeautifulSoup 版本 4 。版本 3 已于多年前停产,并包含未修复的错误。BeautifulSoup 4 还提供了您可能想要使用的附加功能。
BeautifulSoup is pure Python and easily installed into a virtual environment on any platform supported by Python. You are nottied to the system-supplied package here.
BeautifulSoup 是纯 Python,可以轻松安装到 Python 支持的任何平台上的虚拟环境中。您在此处与系统提供的软件包无关。
On Debian Squeezy for example, you'd be stuck with BeautifulSoup 3.1.0, and even the BeautifulSoup developers do not want you to use it!. Your problem with findAllalmost certainly stems from using that release.
例如,在 Debian Squeezy 上,您会被 BeautifulSoup 3.1.0 困住,甚至BeautifulSoup 开发人员也不希望您使用它!. 您的问题findAll几乎肯定源于使用该版本。

