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>
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>
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!