How to Set a Custom User Agent with urllib2.urlopen?

Linda Hamilton
Release: 2024-10-30 13:07:26
Original
803 people have browsed it

How to Set a Custom User Agent with urllib2.urlopen?

Setting a Custom User Agent with urllib2.urlopen

urllib2.urlopen uses the default user agent in Python versions prior to 3.x. This may cause certain websites to restrict access or display different content. You may need to set a custom user agent to bypass these restrictions or access specific content.

Solution

As mentioned in a previous response, you can modify the HTTP headers to set a custom user agent using urllib2.build_opener. Here's an example:

<code class="python">import urllib2

# Create an opener with a custom User-Agent header.
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]

# Open the URL with the opener.
response = opener.open('http://www.stackoverflow.com')</code>
Copy after login

Note: urllib2.urlopen is deprecated in Python 3.x. For Python 3, you can use urllib.request.urlopen instead. The process to set a custom user agent remains the same:

<code class="python">import urllib.request

# Create an opener with a custom User-Agent header.
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]

# Open the URL with the opener.
response = opener.open('http://www.stackoverflow.com')</code>
Copy after login

The above is the detailed content of How to Set a Custom User Agent with 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template