与美丽的汤和硒得到空列表

时间:2019-03-07 09:36:15

标签: python selenium web-scraping beautifulsoup screen-scraping

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

我想从该网站获取TOMATOMETER和AUDIENCE分数, 但有一个空列表。

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

2 个答案:

答案 0 :(得分:1)

您可以将最后一个选择器用于父类的跨度类型。这是使用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)

答案 1 :(得分:0)

您的代码效果很好

>>> 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变量?