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

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)