本帖最后由 suchocolate 于 2021-8-14 13:11 编辑 import urllib.request
# 方法1: 直接用创建的opener打开。
url = "https://www.whatismyip.com"
proxy_support = urllib.request.ProxyHandler({'http': '125.108.106.64:9000'})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-agent', 'Firefox')]
response = opener.open(url)
html = response.read().decode('utf-8')
print(html)
# 方法2:让创建的opener变成默认opener,然后用默认的urlopen方法打开。
url = "https://www.whatismyip.com"
proxy_support = urllib.request.ProxyHandler({'http': '125.108.106.64:9000'})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-agent', 'Firefox')]
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')
print(html)
|