Python TypeError: 'NoneType' 对象不可调用,BeautifulSoup

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21696264/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:25:18  来源:igfitidea点击:

TypeError: 'NoneType' object is not callable, BeautifulSoup

pythonbeautifulsoup

提问by Alpinestar22

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.

我遇到了一个奇怪的错误。我正在尝试做一些基本的解析。本质上,我正在以“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)

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.

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

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

采纳答案by Jayanth Koushik

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

您不需要再次解析 div。尝试这个:

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