Changing User Agent in Python 3 for urrlib.request.urlopen

前端 未结 4 1022
你的背包
你的背包 2020-11-28 07:39

I want to open a url using urllib.request.urlopen(\'someurl\'):

with urllib.request.urlopen(\'someurl\') as url:
b = url.read()
<
4条回答
  •  误落风尘
    2020-11-28 08:35

    I just answered a similar question here: https://stackoverflow.com/a/43501438/206820

    In case you just not only want to open the URL, but also want to download the resource(say, a PDF file), you can use the code as below:

        # 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:

提交回复
热议问题