Changing user agent on urllib2.urlopen

后端 未结 9 2191
感动是毒
感动是毒 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:48

    I answered a similar question a couple weeks ago.

    There is example code in that question, but basically you can do something like this: (Note the capitalization of User-Agent as of RFC 2616, section 14.43.)

    opener = urllib2.build_opener()
    opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
    response = opener.open('http://www.stackoverflow.com')
    

提交回复
热议问题