Python ';非类型';对象不可调用。使用get_text时出现beautifulsoup错误

Python ';非类型';对象不可调用。使用get_text时出现beautifulsoup错误,python,beautifulsoup,typeerror,Python,Beautifulsoup,Typeerror,我编写了以下代码,用于从网页中提取所有文本: from BeautifulSoup import BeautifulSoup import urllib2 soup = BeautifulSoup(urllib2.urlopen('http://www.pythonforbeginners.com').read()) print(soup.get_text()) 问题是我得到了这个错误: print(soup.get_text()) TypeError: 'NoneType' object

我编写了以下代码,用于从网页中提取所有文本:

from BeautifulSoup import BeautifulSoup
import urllib2

soup = BeautifulSoup(urllib2.urlopen('http://www.pythonforbeginners.com').read())
print(soup.get_text())
问题是我得到了这个错误:

print(soup.get_text())
TypeError: 'NoneType' object is not callable

你知道怎么解决这个问题吗

该方法称为
soup.getText()
,即camelCased


为什么你得到的是
TypeError
而不是
AttributeError
,这对我来说是个谜

正如Markku在评论中所建议的,我建议您将代码分解

from BeautifulSoup import BeautifulSoup
import urllib2

URL = "http://www.pythonforbeginners.com"
page = urllib2.urlopen('http://www.pythonforbeginners.com')
html = page.read()
soup = BeautifulSoup(html)
print(soup.get_text())
如果它仍然不起作用,请输入一些打印语句以查看发生了什么

from BeautifulSoup import BeautifulSoup
import urllib2

URL = "http://www.pythonforbeginners.com"
print("URL is {} and its type is {}".format(URL,type(URL)))
page = urllib2.urlopen('http://www.pythonforbeginners.com')
print("Page is {} and its type is {}".format(page,type(page))
html = page.read()
print("html is {} and its type is {}".format(html,type(html))
soup = BeautifulSoup(html)
print("soup is {} and its type is {}".format(soup,type(soup))
print(soup.get_text())

一步一个脚印。。。首先查看
urllib2.urlopen()的结果http://www.pythonforbeginners.com“)
getText()
别名为
get_text()
,所以这不是它。这可能是版本特定的-我得到了与v3.2.1中的OP相同的错误,通过更改为
getText()
可以修复。很高兴知道。我有4x,但由于他使用的是来自BeautifulSoup的
导入,我们可以确定他没有!你可能是对的!