这篇文章主要介绍了关于python3 requests中使用ip代理池随机生成ip的实例,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

啥也不说了,直接上代码吧!

# 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) # 输出返回的内容
登录后复制

相关推荐:

对python使用http、https代理的实例讲解

python使用代理ip访问网站

以上就是python3 requests中使用ip代理池随机生成ip的实例的详细内容,更多请关注Work网其它相关文章!

08-29 00:11