Urlretrieve and User-Agent? - Python

后端 未结 4 1113
暖寄归人
暖寄归人 2021-02-05 13:08

I\'m using urlretrieve from the urllib module.

I cannot seem to find how to add a User-Agent description to my requests.


Is it possible with urlretrieve? o

4条回答
  •  情深已故
    2021-02-05 14:02

    You can use URLopener or FancyURLopener classes. The 'version' argument specifies the user agent of the opener object.

    opener = FancyURLopener({}) 
    opener.version = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.69 Safari/537.36'
    opener.retrieve('http://example.com', 'index.html')
    

提交回复
热议问题