如何修复 python 中的“TypeError: 'NoneType' object is not callable”

问题描述 投票:0回答:1

当我尝试运行这个简单的 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)

尝试运行代码,希望它能像我正在观看的教程中那样运行得很好,但事实并非如此。

python web-scraping beautifulsoup python-requests typeerror
1个回答
0
投票

你有一个错字。将

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)
© www.soinside.com 2019 - 2024. All rights reserved.