Kub*_*bow 7 python request python-2.7
我想知道我在这里做错了什么,但我一直迷路......
在python 2.7中,我正在运行以下代码:
>>> import requests
>>> req = requests.request('GET', 'https://www.zomato.com/praha/caf%C3%A9-a-restaurant-z%C3%A1ti%C5%A1%C3%AD-kunratice-praha-4/daily-menu')
>>> req.content
'<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n'
Run Code Online (Sandbox Code Playgroud)
如果我在浏览器中打开这个,它会正确响应.我正在挖掘并找到类似的urllib库(urllib.request.urlopen 500错误),但我无法适应它,更多我想在这里使用请求.
我可能会在这里找到一些丢失的代理设置,例如这里建议的(Perl File :: Fetch Failed Failed Failed:500 Internal Server Error),但有人可以解释一下,这个问题的正确解决方法是什么?
与浏览器请求不同的一件事是User-Agent; 但是您可以使用以下请求更改它:
url = 'https://www.zomato.com/praha/caf%C3%A9-a-restaurant-z%C3%A1ti%C5%A1%C3%AD-kunratice-praha-4/daily-menu'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.90 Safari/537.36'}
response = requests.get(url, headers=headers)
print(response.status_code) #should be 200
Run Code Online (Sandbox Code Playgroud)