【发布时间】:2016-10-10 12:13:21
【问题描述】:
我需要使我的代码向后兼容 python2.6 和 BeautifulSoup 3。我的代码是使用 python2.7 编写的,在本例中使用 BS4。但是当我尝试在 squeezy 服务器上运行它时,我得到了这个错误(它有 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
如果我改为:
p = soup.body.div.findAll('p')
然后我得到这个错误:
p = soup.body.div.findAll('p')
TypeError: 'NoneType' object is not callable
更新抛出的错误
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
无论哪种方式,这两种方法都适用于我的带有 python2.7 和 bs4 的 Ubuntu,但不适用于 squeezy。那些我看不到/不知道并给我这个错误的版本之间是否还有其他区别?
【问题讨论】:
-
仅使用版本 4 语法时,回退到
from BeautifulSoup import BeautifulSoup(版本 3)是没有意义的。 -
你应该看到我写的我尝试使用向后兼容的语法,但仍然得到同样的错误。
标签: python beautifulsoup backwards-compatibility