注册
登录
 文档中心 产品介绍 开发指南 API接口 代码样例 使用帮助
Python2使用urllib2调用Http代理IP的代码样例
站大爷 官方 2024-01-20 1406 浏览

温馨提示:

1.  urllib2的代码样例同时支持访问http和https网页

2.  运行环境要求 python2.6 / 2.7

3.  代码样例中的代理IP和端口均为虚构,请通过站大爷的API提取链接获取代理IP后使用,避免报错


import urllib2
import ssl

# 全局取消证书验证避免访问https网页报错
ssl._create_default_https_context = ssl._create_unverified_context

# 用户名密码授权
username="username"
password="password"
proxies={
	"http":"http://username:password@168.168.168.168:12345,
	"https":http://username:password@168.168.168.168:12345
}

# 终端IP授权(需提前绑定终端IP)
#proxies={
#	"http":"http://168.168.168.168:12345",
#	"https":"http://168.168.168.168:12345"
 #}

# 要访问的目标网页
target_url="https://example.com"

# 使用代理IP发送请求
proxy_support=urllib2.ProxyHandler(proxies)
opener = urllib2.build_opener(proxy_support)
urllib.request.install_opener(opener)
response=urllib2.urlopen(target_url)

# 获取页面内容
if response.code==200:
	print(response.read().decode('utf-8'))
立即注册站大爷用户,免费试用全部产品
立即注册站大爷用户,免费试用全部产品