Urlretrieve and User-Agent? - Python

后端 未结 4 1111
暖寄归人
暖寄归人 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 13:45

    I know this issue had been there for 7 years. And I reached this issue by trying to figure out how to change the User-Agent while using urlretrieve function.

    To anyone who reached this issue by no luck, here is how I did:

        # proxy = ProxyHandler({'http': 'http://192.168.1.31:8888'})
        proxy = ProxyHandler({})
        opener = build_opener(proxy)
        opener.addheaders = [('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30')]
        install_opener(opener)
    
        result = urlretrieve(url=file_url, filename=file_name)
    

    The reason I added proxy is to monitor the traffic in Charles, and here is the traffic I got:

提交回复
热议问题