在 Python 3 中为 urrlib.request.urlopen 更改用户代理 [英] Changing User Agent in Python 3 for urrlib.request.urlopen

查看:60
本文介绍了在 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'))

这篇关于在 Python 3 中为 urrlib.request.urlopen 更改用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆