18

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?

mihasa
  • 967
  • 2
  • 10
  • 20

1 Answers1

28

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.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • doesn't work for me. Is there a way to find out what the headers need to look like for it to work in every case? Or is something else going on like the site ignoring my headers and going off my IP anyways? – J.Doe Nov 21 '22 at 17:38
  • @J.Doe you can examine the request headers from your web browser and replicate them in the `headers` dict above. Just google ` developer tools headers` and it should come right up. Make sure you're looking at the *request* headers, as it will likely also display the response headers as well. – MattDMo Nov 21 '22 at 17:46
  • Doing that right now. For some reason I can't find the GET request I send in the "Network" tab in Brave, so I'm using Mozilla now, since I know where to find it there. Is Brave not Chromium based as well? Strange that I can't see that in the Network tab... – J.Doe Nov 21 '22 at 17:48
  • @J.Doe you have to reload the page while the dev tools are open, then select one of the requests. There will be multiple, one each for each CSS and JS file, each image, trackers, ads, etc. – MattDMo Nov 21 '22 at 17:50
  • nvm it's the top one, I'm just blind and mozilla displays them a bit more neatly. I didn't look in the right place since I was too used to looking at it through mozilla and not brave. did try it though and removed the "de" from accept-language so that only US and en specifiers remain. Webpage still loads mostly in german. Maybe I'm requesting the wrong thing? The lower part of the webpage is english now, but the middle part where all the data is that I want is in german... – J.Doe Nov 21 '22 at 17:58
  • @J.Doe I'm not sure why that's happening, there likely is another language specifier someplace, perhaps buried in a cookie or something. At any rate, you're getting a proper response, so that's good. Cheers! – MattDMo Nov 21 '22 at 18:06