Again urllib.error.HTTPError: HTTP Error 400: Bad Request

前端 未结 2 1006
被撕碎了的回忆
被撕碎了的回忆 2021-01-12 08:15

Hy! I tried to open web-page, that is normally opening in browser, but python just swears and does not want to work.

import urllib.request, urllib.error
f =          


        
2条回答
  •  耶瑟儿~
    2021-01-12 09:00

    They are probably blocking the fact that it isn't coming from a browser. You probably need a valid User-Agent header or something.

    Using requests, this works:

    import requests
    headers = 
    {
     'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/37.0.2049.0 Safari/537.36'
    }
    
    r = requests.get('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphire', headers=headers)
    print r
    print r.headers
    

提交回复
热议问题