Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python change Accept-Language using requests

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?

like image 934
mihasa Avatar asked May 31 '15 18:05

mihasa


People also ask

How do I set accept-language?

The user can change the Accept-Language header sent by the browser using the browser's preference settings. E.g., in Chrome, go to “Settings”, click on “Show advanced settings...”, scroll down to “Languages”, click on “Language and input settings...”, and then add languages and drag to order them.

What is Http_accept_language?

The HTTP_ACCEPT_LANGUAGE header is defined by W3C in RFC 2616, as “the set of natural languages that are preferred as a response to the request”. It returns one or more languages in the header that tell the web server which language(s) the browser prefers to receive content assets in.

How do I accept a language header?

The Accept-Language request HTTP header indicates the natural language and locale that the client prefers. The server uses content negotiation to select one of the proposals and informs the client of the choice with the Content-Language response header.

Does requests work with Python 3?

The Requests library is available for both Python 2 and Python 3 from the Python Package Index (PyPI), and has the following features: Allows you to send HTTP/1.1 PUT, DELETE, HEAD, GET and OPTIONS requests with ease.


1 Answers

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.

like image 75
MattDMo Avatar answered Sep 24 '22 12:09

MattDMo