我是一个绝对的初学者。我尝试使用 BeautifulSoup 并抓取一个网站。我确实得到了 HTML,但我现在想得到所有具有 content_class
类的 div
。
这是我的尝试:
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 (most recent call last): File "scrape.py", line 11, in soup.find_all('div', class_="content_class") TypeError: 'NoneType' object is not callable
我做错了什么?
最佳答案
您正在使用 BeautifulSoup version three ,但似乎遵循 BeautifulSoup 的文档 version four . Element.find_all()
method仅在最新的主要版本中可用(称为 Element.findAll()
in version 3 )。
我强烈建议你升级:
pip install beautifulsoup4
和
from bs4 import BeautifulSoup
第 3 版已于 2012 年停止接收更新;它现在已经严重过时了。
关于python - 用 BeautifulSoup 抓取网站 : TypeError: 'NoneType' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44875219/