Python 使用请求更改 Accept-Language [英] Python change Accept-Language using requests

查看:44
本文介绍了Python 使用请求更改 Accept-Language的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,并尝试使用请求库从 IMDb 获取一些信息.我的代码正在以我的母语捕获所有数据(例如,电影片名),但我想用英语获取它们.我如何更改请求中的接受语言来做到这一点?

I'm new to python and trying to get some infos from IMDb using requests library. My code is capturing all data (e.g., movie titles) in my native language, but i would like to get them in english. How can i change the accept-language in requests to do that?

推荐答案

您需要做的就是定义自己的标题:

All you need to do is define your own headers:

import requests

url = "http://www.imdb.com/title/tt0089218/"
headers = {"Accept-Language": "en-US,en;q=0.5"}
r = requests.get(url, headers=headers)

您也可以添加任何其他想要修改的标题.

You can add whatever other headers you'd like to modify as well.

这篇关于Python 使用请求更改 Accept-Language的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆