问题
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