Changing user agent on urllib2.urlopen

后端 未结 9 2198
感动是毒
感动是毒 2020-11-22 13:59

How can I download a webpage with a user agent other than the default one on urllib2.urlopen?

9条回答
  •  独厮守ぢ
    2020-11-22 14:51

    Setting the User-Agent from everyone's favorite Dive Into Python.

    The short story: You can use Request.add_header to do this.

    You can also pass the headers as a dictionary when creating the Request itself, 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).

提交回复
热议问题