Home  >  Article  >  Backend Development  >  How to Change the User Agent for `urllib2.urlopen`?

How to Change the User Agent for `urllib2.urlopen`?

DDD
DDDOriginal
2024-11-03 05:53:03372browse

How to Change the User Agent for `urllib2.urlopen`?

Alter User Agent for urllib2.urlopen

Issue: How can one download a webpage using urllib2.urlopen with a non-default user agent?

Answer: A similar question was addressed recently. The following code snippet illustrates how to modify the user agent:

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

Note that 'User-Agent' should be capitalized per RFC 2616, section 14.43.

The above is the detailed content of How to Change the User Agent for `urllib2.urlopen`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn