python requests http response 500 (site can be reached in browser)

后端 未结 3 1889
长情又很酷
长情又很酷 2021-01-02 10:36

I am trying to figure out what I\'m doing wrong here, but I keep getting lost...

In python 2.7, I\'m running following code:

>>> import requ         


        
3条回答
  •  春和景丽
    2021-01-02 10:43

    One thing that is different with the browser request is the User-Agent; however you can alter it using requests like this:

    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
    

    Edit

    Some web applications will also check the Origin and/or the Referer headers (for example for AJAX requests); you can set these in a similar fashion to User-Agent.

    headers = {
        'Origin': 'http://example.com',
        'Referer': 'http://example.com/some_page'
    }
    

    Remember, you are setting these headers to basically bypass checks so please be a good netizen and don't abuse people's resources.

提交回复
热议问题