在 Python 3 中为 urrlib.request.urlopen 更改用户代理

在 Python 3 中为 urrlib.request.urlopen 更改用户代理

问题描述:

我想使用 urllib.request.urlopen('someurl') 打开一个 url:

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

with urllib.request.urlopen('someurl') as url:
b = url.read()

我不断收到以下错误:

urllib.error.HTTPError: HTTP Error 403: Forbidden

我理解错误是由于站点不让 python 访问它,以阻止机器人浪费他们的网络资源 - 这是可以理解的.我去搜索发现你需要更改urllib的用户代理.然而,我为这个问题找到的关于如何更改用户代理的所有指南和解决方案都使用 urllib2,而我使用的是 python 3,所以所有解决方案都不起作用.

I understand the error to be due to the site not letting python access it, to stop bots wasting their network resources- which is understandable. I went searching and found that you need to change the user agent for urllib. However all the guides and solutions I have found for this issue as to how to change the user agent have been with urllib2, and I am using python 3 so all the solutions don't work.

如何使用 python 3 解决这个问题?

How can I fix this problem with python 3?

来自 Python 文档:

import urllib.request
req = urllib.request.Request(
    url, 
    data=None, 
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
    }
)

f = urllib.request.urlopen(req)
print(f.read().decode('utf-8'))