当我尝试运行这个简单的 python 网页抓取程序(如下所示)时,我收到错误“TypeError:‘NoneType’对象不可调用”。我该如何解决这个问题?
from bs4 import BeautifulSoup
import requests
scrape = requests.get("http://quotes.toscrape.com")
soup = BeautifulSoup(scrape.text, "html.parser")
quotes = soup.fundAll("span", attrs={"class":"text"})
authors = soup.findAll("small", attrs={"class":"authors"})
for quote in quotes:
print(quote.text)
for author in authors:
print(author.text)
尝试运行代码,希望它能像我正在观看的教程中那样运行得很好,但事实并非如此。
你有一个错字。将
quotes = soup.fundAll()
更改为 quotes = soup.findAll()
。
这是更正后的版本:
from bs4 import BeautifulSoup
import requests
scrape = requests.get("http://quotes.toscrape.com")
soup = BeautifulSoup(scrape.text, "html.parser")
quotes = soup.findAll("span", attrs={"class":"text"})
authors = soup.findAll("small", attrs={"class":"authors"})
for quote in quotes:
print(quote.text)
for author in authors:
print(author.text)