3 Star 2 Fork 1

kuaidaili/python-sdk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
py3_urllib.py 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
xuanyuwu 提交于 3年前 . token update
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
使用urllib请求代理服务器
请求http和https网页均适用
"""
import urllib.request
import ssl
# 全局取消证书验证,避免访问https网页报错
ssl._create_default_https_context = ssl._create_unverified_context
# 提取代理API接口,获取1个代理IP
api_url = "http://dps.kdlapi.com/api/getdps/?secret_id=o1fjh1re9o28876h7c08&signature=xxxxx&num=1&pt=1&sep=1"
# 获取API接口返回的IP
proxy_ip = urllib.request.urlopen(api_url).read().decode('utf-8')
# 用户名密码认证(私密代理/独享代理)
username = "username"
password = "password"
proxies = {
"http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
"https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
}
# 白名单方式(需提前设置白名单)
# proxies = {
# "http": "http://%(proxy)s/" % {"proxy": proxy_ip},
# "https": "http://%(proxy)s/" % {"proxy": proxy_ip}
# }
# 要访问的目标网页
target_url = "https://dev.kdlapi.com/testproxy"
# 使用代理IP发送请求
proxy_support = urllib.request.ProxyHandler(proxies)
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
response = urllib.request.urlopen(target_url)
# 获取页面内容
if response.code == 200:
print(response.read().decode('utf-8'))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/kuaidaili/python-sdk.git
git@gitee.com:kuaidaili/python-sdk.git
kuaidaili
python-sdk
python-sdk
master

搜索帮助