您的位置:首页 > 编程语言 > Python开发

python3中requests中使用ip代理池随机生成ip

2017-06-29 10:24 656 查看
# encoding:utf-8
import requests  # 导入requests模块用于访问测试自己的ip
import random

pro = ['1.119.129.2:8080', '115.174.66.148', '113.200.214.164']  # 在(http://www.xicidaili.com/wt/)上面收集的ip用于测试
# 没有使用字典的原因是 因为字典中的键是唯一的 http 和https 只能存在一个 所以不建议使用字典

#  你的请求头信息
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
url = 'http://www.whatismyip.com.tw/'  # 你用于测试自己ip的网站
request = requests.get(url, proxies={'http': random.choice(pro)}, headers=head)  # 让问这个网页  随机生成一个ip
request.encoding = request.apparent_encoding  # 设置编码 encoding 返回的是请求头编码  apparent_encoding 是从内容网页中分析出的响应内容编码方式
print(request.text)  # 输出返回的内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: