Home > Software design >  Response code 404 in python requests, opens normally in the browser
Response code 404 in python requests, opens normally in the browser

Time:09-23

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]>

CodePudding user response:

try with requests session:

import requests
from bs4 import BeautifulSoup

url = "https://www.shararam.ru/su/Build/2022.09.20.json"

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0'}
session = requests.Session()
r = session.get(url, timeout=30, headers=headers)     

print(r.status_code) 
200

print(r.json())

{'companyName': 'NewMedia',
 'productName': 'Shararam',
 'productVersion': '0.2',
 'dataUrl': '2022.09.20.data.unityweb',
 'wasmCodeUrl': '2022.09.20.wasm.code.unityweb',
 'wasmFrameworkUrl': '2022.09.20.wasm.framework.unityweb',
 'graphicsAPI': ['WebGL 2.0', 'WebGL 1.0'],
 'webglContextAttributes': {'preserveDrawingBuffer': False},
 'splashScreenStyle': 'Dark',
 'backgroundColor': '#231F20',
 'developmentBuild': False,
 'multithreading': False,
 'unityVersion': '2019.4.21f1'}

CodePudding user response:

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.

  • Related