啥也不說了,直接上代碼吧!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# encoding:utf-8 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的網(wǎng)站 request = requests.get(url, proxies={'http': random.choice(pro)}, headers=head) # 讓問這個網(wǎng)頁 隨機生成一個ip request.encoding = request.apparent_encoding # 設置編碼 encoding 返回的是請求頭編碼 apparent_encoding 是從內(nèi)容網(wǎng)頁中分析出的響應內(nèi)容編碼方式 print(request.text) # 輸出返回的內(nèi)容 |
以上這篇python3 requests中使用ip代理池隨機生成ip的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_39248703/article/details/73871463