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

Setting the User-Agent从大家最喜欢的Dive Into Python


您还可以在创建请求本身时将头作为字典传递,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/", while urllib2‘s default user agent string is "Python-urllib/2.6" (on Python 2.6).

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