如何使用非默认用户代理下载网页urllib2.urlopen
?
urllib2.urlopen
is not available in Python 3.x /q/2792650; the 3.x equivalent is urllib.request.urlopen
. See Changing User Agent in Python 3 for urrlib.request.urlopen https://stackoverflow.com/questions/24226781 to set the user agent in 3.x with the standard library HTTP facilities.
I answered https://stackoverflow.com/a/762007/3790126 a 类似的问题 https://stackoverflow.com/questions/761978/send-headers-along-in-python几周前。
该问题中有示例代码,但基本上您可以执行以下操作:(注意大小写User-Agent
as of RFC 2616 https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html,第 14.43 节。)
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
response = opener.open('http://www.stackoverflow.com')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)