Home > Article > Backend Development > 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!