python – 在urllib2.urlopen上更改用户代理

如何下载一个网页与除urllib2.urlopen上的默认用户代理之外的用户代理?
最佳答案
Setting the User-Agent从大家最喜欢的Dive Into Python

短篇小说:你可以使用Request.add_header来做到这一点。

您还可以在创建请求本身时将头作为字典传递,as the docs note

headers should be a dictionary, and will be treated as if add_header() was called with each key and value as arguments. This is often used to “spoof” the User-Agent header, which is used by a browser to identify itself – some HTTP servers only allow requests coming from common browsers as opposed to scripts. For example, Mozilla Firefox may identify itself as "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11", while urllib2‘s default user agent string is "Python-urllib/2.6" (on Python 2.6).

转载注明原文:python – 在urllib2.urlopen上更改用户代理 - 代码日志