33import time
44import threading
55from queue import Queue
6- def loadip ():
6+ def loadip ():##从代理ip中获取ip 一次若干扩充到queue中
77 url2 = 'http://piping.mogumiao.com/proxy/api/get_ip_al?appKey=f16367295e284173ae450fc38d9098b3&count=20&expiryDate=0&format=1&newLine=2'
88 req = requests .get (url2 )
99 date = req .json ()
1010 if (date ['code' ])!= '3001' :
1111 ipdate2 = date ['msg' ]
12- global ipdate
13- ipdate .extend (ipdate2 )
1412 for va in ipdate2 :
1513 que .put (va )
16- print (ipdate )
1714
18- class downspider (threading .Thread ):
15+ class downspider (threading .Thread ):##线程类
1916 def __init__ (self , threadname , que ):
2017 threading .Thread .__init__ (self )
2118 self .threadname = threadname
@@ -25,20 +22,17 @@ def run(self):
2522 print ('start thread' + self .threadname )
2623 while True :
2724 try :
28- print (self .name ,end = '' )
29- toupiaospider (que ,self .threadname )
25+ toupiaospider (que ,self .threadname )##投票函数
3026 except Exception as e :
3127 print (e ,'888' )
3228 break
33- def getproxies ():
34- b = ipdate [0 ]
29+ def getproxies ():#获取ip 拼接成需要的代理格式
3530 b = que .get ()
3631 d = '%s:%s' % (b ['ip' ], b ['port' ])
3732 global proxies
3833 proxies ['http' ] = d
39- global msg
40- msg = b
41- return proxies
34+ proxies2 = {'http' :d }
35+ return proxies2
4236def toupiaospider (que ,threadname ):
4337 if (que .qsize () < 15 ): # 拓展ip池
4438 loadip ()
@@ -49,16 +43,13 @@ def toupiaospider(que,threadname):
4943 req = requests .post (url , headers = header , data = formData , proxies = proxies2 , timeout = 1.5 )
5044 res = req .json ()
5145 if res ['res' ]== 2001 or req .status_code != 200 :
52- #ipdate.remove(msg)
5346 continue
54- print (threadname ,res ,que .qsize ())
47+ print (threadname ,proxies2 [ 'http' ], res ,que .qsize ())
5548 except Exception as e :
5649 print ('errror' ,e )
57- # ipdate.remove(msg)
5850
5951if __name__ == '__main__' :
60- ipdate = []
61- msg = {}
52+
6253 proxies = {'http' : '' }
6354 stadus = 0
6455 que = Queue ()
@@ -76,19 +67,12 @@ def toupiaospider(que,threadname):
7667 }
7768 loadip ()
7869 time .sleep (5 )
79- threadList = ['thread-1' ,'thread-2' ,'thread-3' ,'thread-4' ,'thread-4' ]
70+ ##线程数组 ->启动 ——>等待join
71+ threadList = ['thread-1' ,'thread-2' ,'thread-3' ,'thread-4' ,'thread-4' ,'thread-5' ]
8072 for j in threadList :
8173 thread = downspider (j , que )
8274 thread .start ()
8375 threads .append (thread )
8476 for t in threads :
8577 t .join ()
86- # for i in range(100):
87- # try:
88- # toupiaospider()
89- # except Exception as e:
90- # print('error')
91- # try:
92- # ipdate.remove(msg)
93- # except Exception as e2:
94- # print("e2",e2)
78+
0 commit comments