源代码附带【注释】:

#-*-编码:UTF-8-*-''Python 3.x描述:本演示演示了使用番茄代理IP请求网页的过程。代码采用多线程逻辑:每隔5秒从API接口获取IP,为每个IP启动一个线程,抓取网页''导入请求的源代码;导入时间;导入线程;导入urllib3IPS =[];# Class爬网线程:def _ _ init _ _: super。_ _ init _ _self . prox yip = prox yip;def run:# Start timing Start = time . time;#消除关闭证书验证urllib3.disable_warnings的警告;#使用代理IP请求URL。注意第三个参数verify=False表示跳过SSL验证HTML = requests . get . content . decode # end timing end = time . time;#输出内容print.getName+"使用代理IP,需要时间"+str+"毫秒"+self . proxip+"获取以下HTML内容: n "+HTML+" n * * * * * * * * * " #获取代理IP ClassGetIP线程的线程类:def self . fetchSecond = fetchSecond;def run:全球ips而True: #获取IP列表RES = requests . get . content . decode #划分获取的IP ips = res.split根据 n;# Use every IP for proxy IP in IPS:if proxy IP . strip = = ' ':continue print #启动线程爬网thread . start;#睡眠时间.睡眠;如果_ _ name _ _ =' _ _ main _ _': #此处请填写您的API接口API URL = " http://x . fanqieip . com/gg gettype = 2 qty = 1 port = 1 time = 1 city = 0 format = 1 #要抓取的目标网站的目标URL = " http://myip . ipip . net/" #获取ip时间间隔,建议为5秒fetchSecond = 5;# Start自动获取IP GetIpThread.start
结果如下:
e: python test venv scripts python.exe e:/python test/venv/scripts/test 125 . 78 . 224 . 148:45144 Thread-2使用代理IP,125.78.224.148:45144用了0.3271493911743164ms得到以下HTML内容:当前test125.78.224.148发自:中国福建莆田电信* * * * * * * * 119 . 96 . 223 . 6:45603 Thread-3使用代理IP,It 中国* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


