【问题标题】:beautifulsoup always returns NoneTypebeautifulsoup 总是返回 NoneType
【发布时间】:2015-11-11 01:07:08
【问题描述】:

beautifulsoup 似乎有问题,无论我搜索什么,它都不会返回任何东西。示例:

from BeautifulSoup import BeautifulSoup
import urllib2
url="http://www.google.com"

html=urllib2.urlopen(url).read()
print type(html)
soup= BeautifulSoup(html)
soup.find_all('div')

返回:

C:\Users\Alexis\Desktop>ipython scrape.py
<type 'str'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\Alexis\Desktop\scrape.py in <module>()
      7 print type(html)
      8 soup= BeautifulSoup(html)
----> 9 soup.find_all('div')
     10
     11

TypeError: 'NoneType' object is not callable

我已尝试升级到最新版本。没有变化。

【问题讨论】:

    标签: beautifulsoup


    【解决方案1】:

    将您的导入行更改为此以使用 BeautifulSoup4:

    from bs4 import BeautifulSoup
    

    将程序的最后一行更改为此以打印出divs:

    for div in soup.find_all('div'):
        print('here comes another div')
        print(div)
    

    【讨论】:

    • 相同。我得到“Nonetype”对象在循环设置时不可调用。看来我不能使用 find_all 或任何 beautifulsoup 方法,因为 soup 似乎没有被识别为 beautifulsoup 对象。
    • 您在使用 BeautifulSoup 3 吗?我认为您应该使用 BeautifulSoup 4 - 我的导入行是这样的:from bs4 import BeautifulSoup
    • 你是对的,在我的导入解决问题之前添加'from bs4'。谢谢。
    • 谢谢,很高兴听到这个消息。
    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 2013-07-30
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    相关资源
    最近更新 更多