Search code examples
pythonpython-requestshttprequest

Response code 404 in python requests, opens normally in the browser


I'm trying to requests.get this page in python requests but get error code 404. It opens normally in the browser. What's wrong here?

requests.get('https://www.shararam.ru/su/Build/2022.09.20.json')
<Response [404]>

Solution

  • You should always figure out the correct headers that are needed from the server to accept your request. In my case i tried with:

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36', 
               'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
               'Accept-Language': 'en-US,en;q=0.5',
               'Accept-Encoding': 'gzip, deflate, br'}
    

    And worked. It seems that accept encoding was the key in this case.