加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

urllib2使用代理服务器

发布时间:2020-12-17 17:08:39 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 # The proxy address and port:proxy_info = { 'host' : 'proxy.myisp.com','port' : 3128 } # We create a handler for the proxyproxy_support = ur

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

# The proxy address and port:
proxy_info = { 'host' : 'proxy.myisp.com','port' : 3128
             }
 
# We create a handler for the proxy
proxy_support = urllib2.ProxyHandler({"http" : "http://%(host)s:%(port)d" % proxy_info})
 
# We create an opener which uses this handler:
opener = urllib2.build_opener(proxy_support)
 
# Then we install this opener as the default opener for urllib2:
urllib2.install_opener(opener)
 
# Now we can send our HTTP request:
htmlpage = urllib2.urlopen("http://sebsauvage.net/").read(200000)
 
#如果代理需要验证
proxy_info = { 'host' : 'proxy.myisp.com','port' : 3128,'user' : 'John Doe','pass' : 'mysecret007'
             }
proxy_support = urllib2.ProxyHandler({"http" : "http://%(user)s:%(pass)[email?protected]%(host)s:%(port)d" % proxy_info})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
htmlpage = urllib2.urlopen("http://sebsauvage.net/").read(200000)

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读