网站刮痧与BeautifulSoup:TypeError:' NoneType'对象不可调用

时间:2017-07-02 21:07:17

标签: python web-scraping beautifulsoup

我是一个绝对的初学者。我尝试使用BeautifulSoup并抓取一个网站。我确实获得了HTML,但我想现在获得所有divscontent_class

这是我的尝试:

import requests
from BeautifulSoup import BeautifulSoup

#Request the page and parse the HTML
url = 'mywebsite'
response = requests.get(url)
html = response.content

#Beautiful Soup
soup = BeautifulSoup(html)
soup.find_all('div', class_="content_class")

然而,这不起作用。 我明白了:

  

Traceback(最近一次调用最后一次):文件" scrape.py",第11行,in          soup.find_all(' div',class _ =" content_class")TypeError:' NoneType'对象不可调用

我做错了什么?

2 个答案:

答案 0 :(得分:3)

您正在使用BeautifulSoup version three,但似乎遵循BeautifulSoup version four的文档。 Element.find_all() method仅适用于最新的主要版本(称为Element.findAll() in version 3)。

我强烈建议你升级:

pip install beautifulsoup4

from bs4 import BeautifulSoup

版本3已于2012年停止接收更新;它现在严重过时了。

答案 1 :(得分:1)

你收到此错误是因为BeautifulSoup中没有方法“find_all”,有“findAll”方法,这段代码应该有帮助

 soup.findAll('div', {'class': 'content_class'})