TypeError:' NoneType'对象不可调用,BeautifulSoup [英] TypeError: 'NoneType' object is not callable, BeautifulSoup

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

问题描述

我遇到一个奇怪的错误.我正在尝试进行一些基本的分析.本质上,我正在以"x"格式收集数据,并希望以我可以使用的格式返回所有内容.我的直接问题是我的代码返回了一个奇怪的错误.我在这里浏览过同一问题的其他一些帖子/答案,但出于上下文考虑……确实很难查明问题.

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)

很明显,到此为止,我已经包含了所需的库...当我拉出soup2的代码(在要处理的新文本bb4上的bs4的第二个实例化)时,只需打印函数的输入即可,一切正常.

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.

这是错误:

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

推荐答案

您无需再次解析div.试试这个:

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)

这篇关于TypeError:&amp;#39; NoneType&amp;#39;对象不可调用,BeautifulSoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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