import urllib2 request = urllib2.Request("https://www.example.com") response = urllib2.urlopen(request) print(response.read())
import urllib2 headers = {'User-Agent': 'Mozilla/5.0'} request = urllib2.Request("https://www.example.com", headers=headers) response = urllib2.urlopen(request) print(response.code)This example sets the headers of the request to mimic a browser's user-agent before sending the request. It prints the HTTP status code of the response. Python urllib2 is part of the standard library in Python 2.x, but it has been replaced by the urllib library in Python 3.x.