有一张空单子,上面有靓汤和硒

2024-10-03 04:29:15 发布

您现在位置:Python中文网/ 问答频道 /正文

https://www.rottentomatoes.com/m/the_lord_of_the_rings_the_return_of_the_king

我想从那个网站上得到Tometer和观众评分, 但名单是空的

soup = BeautifulSoup(html, 'html.parser')
notices = soup.select('#tomato_meter_link > span.mop-ratings-wrap__percentage')

Tags: ofthehttpscomreturn网站htmlwww
2条回答

你的代码运行得很好

>>> from bs4 import BeautifulSoup
>>> html = requests.get('https://www.rottentomatoes.com/m/the_lord_of_the_rings_the_return_of_the_king').text
>>> soup = BeautifulSoup(html, 'html.parser')
>>> notices = soup.select('#tomato_meter_link > span.mop-ratings-wrap__percentage')
>>> notices
[<span class="mop-ratings-wrap__percentage">93%</span>]

你是怎么得到html变量的

可以将跨度类型的最后一个子选择器与父类一起使用。这是使用BeautifulSoup 4.7.1

import requests
from bs4 import BeautifulSoup

res = requests.get('https://www.rottentomatoes.com/m/the_lord_of_the_rings_the_return_of_the_king')
soup = bs(res.content, 'lxml')
ratings = [item.text.strip() for item in soup.select('h1.mop-ratings-wrap__score span:last-child')]
print(ratings)

相关问题 更多 >