python typeerror对象不支持_python – TypeError:’NoneType’对象不可调用,BeautifulSoup...

在尝试使用BeautifulSoup解析网页时遇到TypeError: 'NoneType'对象不可调用的错误。问题出现在对一个返回None的变量进行调用。在函数parseDiv中,当尝试将传入的文本转换为BeautifulSoup对象时,由于输入的text可能是None,导致错误。解决方案是检查text是否为None,避免对None进行实例化。
摘要由CSDN通过智能技术生成

我遇到了一个奇怪的错误.我正在尝试做一些基本的解析.基本上,我正在以’x’格式收集数据,并希望以我可以使用的格式返回所有内容.我的直接问题是我的代码返回了一个奇怪的错误.我已经在这里查看了一些其他帖子/答案以查找相同的问题,但是脱离了上下文……确实很难确定问题.

data = url.text

soup = BeautifulSoup(data, "html5lib")

results = [] # this is what my result set will end up as

def parseDiv(text):

#function takes one input parameter - a single div for which it will parse for specific items, and return it all as a dictionary

soup2 = BeautifulSoup(text)

title = soup2.find("a", "yschttl spt")

print title.text

print

return title.text

for result in soup.find_all("div", "res"):

"""

This is where the data is first handled - this would return a div with links, text, etc -

So, I pass the blurb of text into the parseDiv() function

"""

item = parseDiv(result)

results.append(item)

很明显,在这一点上,我已经包含了我需要的库…当我拉出soup2的代码(在我要处理的新文本的第二个实例化bs4)时,只打印我的函数的输入,这一切作品.

这是错误:

Traceback (most recent call last):

File "testdata.py", line 29, in

item = parseDiv(result)

File "testdata.py", line 17, in parseDiv

soup2 = BeautifulSoup(text)

File "C:\Python27\lib\site-packages\bs4\__i

markup = markup.read()

TypeError: 'NoneType' object is not callable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值