python - BeautifulSoup错误AttributeError : 'NoneType' object has no attribute 'getText'

标签 python error-handling beautifulsoup attributeerror

我正在尝试制作一个程序,该程序将从烂番茄的“前100名电影”中删除任意一部电影。然后,我要它打印电影的名称和观众分数。我收到属性错误。

from urllib.request import urlopen
from bs4 import BeautifulSoup 
import random

url = "https://www.rottentomatoes.com/top/bestofrt/"
page = urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
table = soup.find("table",attrs={"class":"table"})
links = table.findAll("a")
titles = []
hyperLinks = []

for link in links:
  titles.append(link.getText().strip())
  hyperLinks.append(link["href"])

choice = random.randint(0,len(hyperLinks) - 1)
page2 = urlopen(url[:-14] + hyperLinks[choice])
soup2 = BeautifulSoup(page2,"html.parser")
span = soup.find("span",attrs={"class":"mop-ratings-wrap__percentage"})
print(titles[choice])
print(span.getText().strip())

错误在我打印跨度的最后一行。

https://www.rottentomatoes.com/top/bestofrt/

最佳答案

用汤换汤2

span = soup2.find("span",attrs={"class":"mop-ratings-wrap__percentage"})

关于python - BeautifulSoup错误AttributeError : 'NoneType' object has no attribute 'getText' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61505096/

相关文章:

python - 我可以在本地计算机上使用Apache Spark处理100 GB的数据吗?

python - 多类 Keras 感知器分类器将所有内容分类为单个类

python - 我无法显示 html 代码 - Beautifulsoup

python - 从下拉菜单python中的每个选项中抓取表格

python - 创建大型对角稀疏矩阵

python - 错误消息缺少必需的依赖项,导入错误 : Missing required dependencies ['numpy' ] when I try and freeze an executable

javascript - 发出异步请求时嵌套错误消息的正确方法

c# - 转换varchar值时转换失败

azure - 如何使用 Azure 数据库模拟暂时性错误?

python - 如何使用 Selenium 或 BeautifulSoup 遍历这些 javascript 链接?