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

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

问题描述

我是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使用请求更改接受语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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