proxyswitch 代理


import urllib.request

proxyswitch = True
#创建一个代理对象
httpproxy_handler = urllib.request.ProxyHandler({"http":"203.174.112.13:3128"})

nullproxy_handler = urllib.request.ProxyHandler({})
# 调用build_opener()方法构建一个自定义的opener对象,参数是构建的处理器对象
if proxyswitch:
    opener = urllib.request.build_opener(httpproxy_handler)
else:
    opener = urllib.request.build_opener(nullproxy_handler)
# 构建了一个全局的opener,之后所有的请求都可以用urlopen()方式去发送,也附带Handler的功能
urllib.request.install_opener(opener)
request = urllib.request.Request("http://www.baidu.com")
respone = urllib.request.urlopen(request)

print(respone.read())

你可能感兴趣的:(script)