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

Python使用BeautifulSoup爬取IMDB返回非英文结果问题排查

问题原因
  • IMDB内置多语言适配规则,会结合请求携带的Accept-Language头、请求IP归属信息,自动返回对应语言版本的页面
  • 使用requests发起请求时默认未自定义请求头,站点匹配后返回了繁体中文版的Top250榜单页,因此提取到的电影名称均为繁体中文,不符合预期的英文结果。
修复方式

发起GET请求时主动添加请求头,将Accept-Language设置为en-US,en;q=0.9,强制站点返回英文版本页面即可。原代码缺少依赖导入语句,修复时一并补上。

修正后完整代码
import requests
from bs4 import BeautifulSoup

# 配置请求头,指定接收英文内容
headers = {
    "Accept-Language": "en-US,en;q=0.9"
}

source = requests.get('https://www.imdb.com/chart/top/', headers=headers)
source.raise_for_status()
soup = BeautifulSoup(source.text, 'html.parser')
movies = soup.find('tbody', class_='lister-list').find_all('tr')

for movie in movies:
    name = movie.find('td', class_='titleColumn').a.text
    rank = movie.find('td', class_='titleColumn').get_text(strip=True).split('.')[0]
    year = movie.find('td', class_='titleColumn').span.text.strip('()')
    rating = movie.find('td', class_="ratingColumn imdbRating").strong.text
    print(name, rank, year, rating)
运行验证

调整后代码提取的内容为英文原版信息,输出片段参考:

The Shawshank Redemption 1 1994 9.2
The Godfather 2 1972 9.2
The Dark Knight 3 2008 9.0
The Godfather Part II 4 1974 9.0
12 Angry Men 5 1957 8.9
Schindler's List 6 1993 8.9

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 14:18:26