我用BeautifulSoup从Metacritic网站上获取游戏数据.我正在try 获取每个 comments 者的分数和文本.我以为一切都很顺利,但当我收到回复时,我看到的是这样的情况:

class="c-siteReviewPlaceholder_header"

该网站在其类中没有占位符一词.我知道我需要针对特定的类:

class_="c-pageProductReviews_row"

我的代码如下所示:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.metacritic.com/game/alien-isolation/critic-reviews/? 
platform=playstation-4'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '\
       'AppleWebKit/537.36 (KHTML, like Gecko) '\
       'Chrome/75.0.3770.80 Safari/537.36'}
 critic_review_page = requests.get(URL, headers=headers)
 soup = BeautifulSoup(critic_review_page.content, "html.parser")
 critic_review_rows = soup.find_all("div", class_="c-pageProductReviews_row")
 print(critic_review_rows)

当我打印critic_review_rows的时候,我发现很多类都有单词占位符.我不知道Metacritic是否不会让我刮掉这个网站,也不知道发生了什么.当我抓取数据时,几乎就像数据没有加载数据一样.

推荐答案

这里的主要问题是,内容是由javascript动态呈现的,requests不能处理,因为它不像浏览器那样工作,只处理第一个静态响应状态.

初始状态存储在页面源代码末尾的脚本中,因此您可以提取它,但更好的方法是使用调用的API:

import requests 

url = 'https://fandom-prod.apigee.net/v1/xapi/reviews/metacritic/critic/games/alien-isolation/platform/playstation-4/web?apiKey=1MOZgmNFxvmljaQR1X9KAij9Mo4xAY3u&offset=0&limit=50&sort=score&componentType=ReviewList'

requests.get(url).json()

{'data': {'id': 1400262756,
  'totalResults': 50,
  'items': [{'quote': 'The permanent threat of death keeps you forced to the ground – we can’t remember the last game where we willingly snuck around so much – and it feels like the claustrophobic corridors and catwalks of the Sevastopol were built from that angle. Being crouched, looking up at everything… it really does give you that feeling that Creative Assembly wanted it all along – that ‘prey being hunted’ effect. It makes a refreshing change from the feeling of being overpowered and able to kill anything that appears.',
    'score': 90,
    'url': 'http://www.play-mag.co.uk/reviews/ps4-reviews/alien-isolation-review-2/',
    'date': '2014-10-03',
    'author': None,
    'authorSlug': None,
    'image': None,
    'publicationName': 'Play UK',
    'publicationSlug': 'play-uk',
    'reviewedProduct': {'id': 1400262756,
     'type': 'games',
     'title': 'Alien: Isolation',
     'url': '/game/alien-isolation/',
     'criticScoreSummary': {'url': '/game/alien-isolation/critic-reviews/?platform=playstation-4',
      'score': 79},
     'platform': {'id': 1500000006, 'name': 'PlayStation 4'},
     'gameTaxonomy': {'game': {'id': 1400262756, 'name': 'Alien: Isolation'},
      'platform': {'id': 1500000006, 'name': 'PlayStation 4'}}},
    'platform': 'PlayStation 4'},...

Python-3.x相关问答推荐

如何创建多个日志(log)文件

如何使用regex将电话号码和姓名从文本字符串中分离出来

ValueError at /register/ 视图authenticate.views.register_user 未返回HttpResponse 对象.它返回 None 相反

从 https://www.niftytrader.in/stock-options-chart/sbin 提取 SBIN 股票最大痛苦值的 Python 代码不起作用 - 我错过了什么?

我正在使用 python 线程,当查询 mysql 时,代码似乎在运行并保持在无限循环中,没有返回任何错误

有没有一种方法可以通过输入从 0 到 255 的 R、G 和 B 值来生成 RGB colored颜色 ,而无需使用 python 中的 matplotlib 模块?

如何在 Telethon 中向机器人发送发送表情符号

Python - 如何从同一台客户端机器运行多个Flask应用程序

合并两个numpy数组

python中是否有大于但小于函数?

如何从左到右解包元组?

如何使我的课程在 Python 中非常可打印?

使用 Python 3 按行进行分析

'~'(波浪号)运算符在 Python 中的应用

python total_ordering:为什么使用 __lt__ 和 __eq__ 而不是 __le__?

PIL 在图像上绘制半透明方形覆盖

python - 使用 matplotlib 和 boto 将绘图从内存上传到 s3

Windows 下 Python 3.x 的 OpenCV

异常被忽略是什么类型的消息?

如何在 Python 3.2 中退出?