[我正在尝试制作一个程序,以从烂番茄《有史以来的前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())
错误在我打印跨度的最后一行。
用汤换汤2
span = soup2.find("span",attrs={"class":"mop-ratings-wrap__percentage"})