类型错误:'NoneType'对象不是可调用,BeautifulSoup [英] TypeError: 'NoneType' object is not callable, BeautifulSoup

查看:2544
本文介绍了类型错误:'NoneType'对象不是可调用,BeautifulSoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的错误。我试图做一些基本的分析。从本质上讲,我聚集在'X'格式的数据,并希望在我可以使用的格式返回的一切。我的眼前的问题是,我的code返回一个奇怪的错误。我已经通过一些在这里同样问题的其他职位/答案看去,却断章取义......它是真正难以确定问题。

 数据= url.text汤= BeautifulSoup(数据,html5lib)结果= []#这就是我的结果集将最终成为高清parseDiv(文本):
    #function需要一个输入的参数 - 一个DIV的,它会解析为具体项目,并返回它作为一个字典
    soup2 = BeautifulSoup(文本)
    标题= soup2.find(一,yschttl SPT)
    打印title.text
    打印    返回title.text对于导致soup.find_all(格,资源):
    
    这是该数据优先处理 - 这将返回的链接,文字等的div -
    所以,我通过文字导语成parseDiv()函数
    
    项目= parseDiv(结果)
    results.append(项目)

显然,在这一点上,我包括我需要的库......当我拉code为soup2(BS4的第二个实例上我的文字要处理的新导语),并只打印输入我的功能,这一切的作品。

以下是错误:

 回溯(最后最近一次调用):
  文件testdata.py,第29行,上述<&模块GT;
    项目= parseDiv(结果)
  文件testdata.py,第17行,在parseDiv
    soup2 = BeautifulSoup(文本)
  文件C:\\ Python27 \\ lib目录\\站点包\\ BS4 \\ __我
    标记= markup.read()
类型错误:'NoneType'对象不是可调用


解决方案

您不必再次解析的div。试试这个:

 在soup.find_all格('格','资源'):
    一个= div.find('一个','yschttl SPT')
    如果一个:
        打印a.text
        打印
        results.append(一)

I'm running into a strange error. I'm trying to do some basic parsing. Essentially, I'm gathering data in 'x' format, and want to return everything in a format that I can use. My immediate issue is that my code is returning a strange error. I have looked through some of the other posts / answers on here for the same issue, but out of context... it is truly hard to pinpoint the issue.

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)

Obviously at this point, I've included my needed libraries... When I pull the code for soup2 (the second instantiation of bs4 on my new blurbs of text to be processed), and just print the input of my function, it all works.

Here is the error:

Traceback (most recent call last):
  File "testdata.py", line 29, in <module>
    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

解决方案

You don't need to parse the divs once again. Try this:

for div in soup.find_all('div', 'res'):
    a = div.find('a', 'yschttl spt')
    if a:
        print a.text
        print
        results.append(a)

这篇关于类型错误:'NoneType'对象不是可调用,BeautifulSoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆