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

前端 未结 2 1008
被撕碎了的回忆
被撕碎了的回忆 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 08:38

    This URL seems to be doing user agent string checking. If I adjust my user agent string in Firefox to Python-urllib/2.7, it fails with the Bad Request you are seeing.

    As you are using urllib, you can adjust the User Agent following this tutorial

    from urllib.request import FancyURLopener
    
    class MyOpener(FancyURLopener):
        version = 'My new User-Agent'   # Set this to a string you want for your user agent
    
    myopener = MyOpener()
    page = myopener.open('http://www.booking.com/reviewlist.html?cc1=tr;pagename=sapphire')
    

提交回复
热议问题