Python BeautifulSoup-类型错误:';非类型';对象不可调用
我需要使我的代码向后兼容python2.6和BeautifulSoup3。我的代码是使用python2.7编写的,在本例中使用的是BS4。但当我试图在squeezy服务器上运行它时,我得到了这个错误(它有python2.6和bs3): 如果我改为:Python BeautifulSoup-类型错误:';非类型';对象不可调用,python,beautifulsoup,backwards-compatibility,Python,Beautifulsoup,Backwards Compatibility,我需要使我的代码向后兼容python2.6和BeautifulSoup3。我的代码是使用python2.7编写的,在本例中使用的是BS4。但当我试图在squeezy服务器上运行它时,我得到了这个错误(它有python2.6和bs3): 如果我改为: p = soup.body.div.findAll('p') 然后我得到这个错误: p = soup.body.div.findAll('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
不管怎样,这两种方法都可以在我的Ubuntu上使用python2.7和bs4,但不能在squeezy上使用。我看不到/不知道这些版本之间是否存在其他差异,并导致我出现此错误?您使用的是BeautifulSoup 3,但使用的是BeautifulSoup 4语法 你的退路在这里是错误的:
try:
from bs4 import BeautifulSoup
except ImportError:
from BeautifulSoup import BeautifulSoup
如果要使用版本3或4,请坚持使用版本3语法:
p = soup.body.div.findAll('p')
因为find_all
在BeautifulSoup 3中不是有效的方法,所以它被解释为标记搜索。HTML中没有find_all
标记,因此返回None
,然后尝试调用
接下来,Beautifulsoup3使用的解析器将对损坏或不完整的HTML做出不同的响应。如果您在Ubuntu上安装了lxml
,那么它将被用作默认解析器,并为您插入一个缺少的
标记。美女三号可能会忽略这一点
我强烈建议您删除后备方案,只使用BeautifulSoup版本4。版本3在几年前已经停止使用,并且包含未修复的bug。BeautifulSoup 4还提供了您可能希望使用的其他功能
BeautifulSoup是纯Python,可以轻松地安装到Python支持的任何平台上的虚拟环境中。您与此处系统提供的软件包没有关联
例如,在Debian Squeezy上,您会被BeautifulSoup 3.1.0,甚至。你的
findAll
问题几乎肯定源于使用该版本。我知道这是一篇6年前的文章,但如果有人有类似问题,就直接发布
似乎在第9行,它应该是一个格式化的字符串,在添加f之后,它似乎工作得非常好
import pandas as pd
import numpy as np
import requests
from bs4 import BeautifulSoup
product_all_pages = []
for i in range(1,15):
response = requests.get(f"https://www.bol.com/nl/s/?page={i}&searchtext=hand+sanitizer&view=list")
content = response.content
parser = BeautifulSoup(content, 'html.parser')
body = parser.body
producten = body.find_all(class_="product-item--row js_item_root")
product_all_pages.extend(producten)
len(product_all_pages)
price = float(product_all_pages[1].meta.get('content'))
productname = product_all_pages[1].find(class_="product-title--inline").a.getText()
print(price)
print(productname)
productlijst = []
for item in product_all_pages:
if item.find(class_="product-prices").getText() == '\nNiet leverbaar\n':
price = None
else:
price = float(item.meta['content'])
product = item.find(class_="product-title--inline").a.getText()
productlijst.append([product, price])
print(productlijst[:3])
df = pd.DataFrame(productlijst, columns=["Product", "price"])
print(df.shape)
df["price"].describe()
当只使用版本4的语法时,从BeautifulSoup导入BeautifulSoup(版本3)返回到
是没有意义的。你应该看到,我写的我尝试使用向后兼容的语法,但仍然得到相同的错误。我的意图是使用bs4。但问题是,squeezy只有bs3,我也需要它在那里工作。但为什么findAll在依赖bs3时不起作用?当我使用bs3时。@Andrius:我正要在你的问题上发表这篇文章:对findAll()
抛出的异常的完整回溯是什么?你确定你在那里复制了正确的异常消息(它与查找所有异常消息相同)?我只是复制/粘贴了使用findAll@Andrius:很有趣,因为我无法用BeautifulSoup 3.2.1重现这一点。我确实有一个Squeezy系统,因为PyPI不再提供3.1.0,我将尝试在那里安装它。@Andrius:这看起来像是3.1.0特定的问题。该版本很快被3.2系列取代,开发者在2009年告诉大家不要使用3.1。不要使用那个版本,你就不会有问题了。
import pandas as pd
import numpy as np
import requests
from bs4 import BeautifulSoup
product_all_pages = []
for i in range(1,15):
response = requests.get(f"https://www.bol.com/nl/s/?page={i}&searchtext=hand+sanitizer&view=list")
content = response.content
parser = BeautifulSoup(content, 'html.parser')
body = parser.body
producten = body.find_all(class_="product-item--row js_item_root")
product_all_pages.extend(producten)
len(product_all_pages)
price = float(product_all_pages[1].meta.get('content'))
productname = product_all_pages[1].find(class_="product-title--inline").a.getText()
print(price)
print(productname)
productlijst = []
for item in product_all_pages:
if item.find(class_="product-prices").getText() == '\nNiet leverbaar\n':
price = None
else:
price = float(item.meta['content'])
product = item.find(class_="product-title--inline").a.getText()
productlijst.append([product, price])
print(productlijst[:3])
df = pd.DataFrame(productlijst, columns=["Product", "price"])
print(df.shape)
df["price"].describe()