'NoneType' object is not callable beautifulsoup error while using get_text

心不动则不痛 提交于 2019-12-23 12:24:34

问题


I wrote this code for extracting all text from a web page:

from BeautifulSoup import BeautifulSoup
import urllib2

soup = BeautifulSoup(urllib2.urlopen('http://www.pythonforbeginners.com').read())
print(soup.get_text())

The problem is I get this error:

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

Any idea about how to solve this?


回答1:


The method is called soup.getText(), i.e. camelCased.

Why you get TypeError instead of AttributeError here is a mystery to me!




回答2:


As Markku suggests in the comments, I would recommend breaking your code up.

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())

If it's still not working, throw in some print statements to see what's going on.

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())


来源:https://stackoverflow.com/questions/21939240/nonetype-object-is-not-callable-beautifulsoup-error-while-using-get-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!