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

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

DDD
Release: 2024-11-03 05:53:03
Original
389 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template