You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用BeautifulSoup抓取Metacritic游戏评测数据遇占位符类问题

解决Metacritic爬虫返回占位符的问题

你的问题核心是Metacritic的动态内容加载或反爬机制导致requests获取的静态页面仅包含占位符,真实评论数据并未在初始HTML中返回。以下是具体解决办法:

1. 补充完整请求头,模拟真实浏览器请求

Metacritic会校验请求头的完整性,仅User-Agent可能不够。补充更多字段让请求更逼真:

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/118.0.0.0 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1'
}
critic_review_page = requests.get(URL, headers=headers)
# 先确认页面是否正常返回真实内容
print(critic_review_page.text[:1000])
soup = BeautifulSoup(critic_review_page.content, "html.parser")
critic_review_rows = soup.find_all("div", class_="c-pageProductReviews_row")
print(len(critic_review_rows))

2. 使用动态渲染工具抓取JS加载的内容

如果补充请求头无效,说明评论数据是通过JavaScript动态加载的,requests无法获取,此时需要用Selenium渲染页面:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import time

URL = 'https://www.metacritic.com/game/alien-isolation/critic-reviews/?platform=playstation-4'
# 配置无头浏览器
chrome_options = Options()
chrome_options.add_argument('--headless=new')
chrome_options.add_argument('--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36')

driver = webdriver.Chrome(options=chrome_options)
driver.get(URL)
# 等待页面加载完成
time.sleep(3)
# 滚动页面加载更多评论(如果需要)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)

soup = BeautifulSoup(driver.page_source, "html.parser")
critic_review_rows = soup.find_all("div", class_="c-pageProductReviews_row")
for row in critic_review_rows:
    # 提取评分和评测文本示例
    score = row.find('div', class_='c-siteReviewScore_background')
    review_text = row.find('div', class_='c-siteReview_reviewText')
    if score and review_text:
        print(f"评分: {score.text.strip()}")
        print(f"评测: {review_text.text.strip()}\n")

driver.quit()

3. 验证目标类名是否变更

Metacritic可能会更新页面CSS类名,打开浏览器开发者工具(F12),查看真实评论行的类名是否还是c-pageProductReviews_row,如果已变更,需要替换为新的类名。

4. 应对反爬限制

如果频繁请求被限制,可以:

  • 每次请求后添加time.sleep(2-5)的间隔
  • 使用代理IP轮换请求
  • 避免短时间内发送大量请求

内容的提问来源于stack exchange,提问作者ravenUSMC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 01:11:23