国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python代理抓取并驗證使用多線程實現(xiàn)

2019-11-25 18:39:14
字體:
供稿:網(wǎng)友
沒有使用隊列,也沒有線程池還在學(xué)習(xí)只是多線程
復(fù)制代碼 代碼如下:

#coding:utf8
import urllib2,sys,re
import threading,os
import time,datetime
'''''
這里沒有使用隊列 只是采用多線程分發(fā)對代理量不大的網(wǎng)頁還行但是幾百幾千性能就很差了
'''
def get_proxy_page(url):
'''''解析代理頁面 獲取所有代理地址'''
proxy_list = []
p = re.compile(r'''''<div>(.+?)<span class="Apple-tab-span" style="white-space:pre">.*?</span>(.+?)<span class="Apple-tab-span" style="white-space:pre">.+?</span>(.+?)(<span.+?)?</div>''')
try:
res = urllib2.urlopen(url)
except urllib2.URLError:
print 'url Error'
sys.exit(1)
pageinfo = res.read()
res = p.findall(pageinfo) #取出所有的
#組合成所有代理服務(wù)器列表成一個符合規(guī)則的list
for i in res:
ip = i[0]
port = i[1]
addr = i[2]
l = (ip,port,addr)
proxy_list.append(l)
return proxy_list
#同步鎖裝飾器
lock = threading.Lock()
def synchronous(f):
def call(*args, **kw):
lock.acquire()
try:
return f(*args, **kw)
finally:
lock.release()
return call
#時間計算器
def sumtime(f):
def call(*args, **kw):
t1 = time.time()
try:
return f(*args, **kw)
finally:
print u'總共用時 %s' % (time.time() - t1)
return call
proxylist = []
reslist = []
#獲取單個代理并處理
@synchronous
def getoneproxy():
global proxylist
if len(proxylist)>0:
return proxylist.pop()
else:
return ''
#添加驗證成功的代理
@synchronous
def getreslist(proxy):
global reslist
if not (proxy in reslist):
reslist.append(proxy)
def handle():
timeout = 10
test_url = r'http://www.baidu.com'
test_str = '030173'
while 1:
proxy = getoneproxy()
#最后一個返回是空
if not proxy:
return
print u"正在驗證 : %s" %proxy[0]
#第一步啟用 cookie
cookies = urllib2.HTTPCookieProcessor()
proxy_server = r'http://%s:%s' %(proxy[0],proxy[1])
#第二步 裝載代理
proxy_hander = urllib2.ProxyHandler({"http":proxy_server})
#第三步 組合request
try:
opener = urllib2.build_opener(cookies, proxy_hander)
pass
except urllib2.URLError:
print u'url設(shè)置錯誤'
continue
#配置request
opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1')]
#發(fā)送請求
urllib2.install_opener(opener)
t1 = time.time()
try:
req = urllib2.urlopen(test_url,timeout=timeout)
result = req.read()
pos = result.find(test_str)
timeused = time.time() - t1
if pos>1:
#保存到列表中
getreslist((proxy[0],proxy[1],proxy[2],timeused))
print u'成功采集',proxy[0],timeused
else:
continue
except Exception,e:
print u'采集失敗 %s :timeout' %proxy[0]
continue
def save(reslist):
path = os.getcwd()
filename = path + '/Proxy-'+datetime.datetime.now().strftime(r'%Y%m%d%H%M%S')+'.txt'
f = open(filename,'w+')
for proxy in reslist:
f.write('%s %s %s %s /r/n'%(proxy[0],proxy[1],proxy[2],proxy[3]))
f.close()
@sumtime
def main():
url = r'http://www.free998.net/daili/httpdaili/8949.html'
global proxylist,reslist
#獲取所有線程
proxylist = get_proxy_page(url)
print u'一共獲取 %s 個代理' %len(proxylist)
#print proxylist
print '*'*80
#線程創(chuàng)建和分發(fā)任務(wù)
print u'開始創(chuàng)建線程處理.....'
threads = []
proxy_num = len(proxylist)
for i in range(proxy_num):
th = threading.Thread(target=handle, args=())
threads.append(th)
for thread in threads:
thread.start()
for thread in threads:
threading.Thread.join(thread)
print u'獲取有效代理 %s 個,現(xiàn)在開始排序和保存 '%len(reslist)
reslist = sorted(reslist,cmp=lambda x,y:cmp(x[3],y[3]))
save(reslist)
if __name__=='__main__':
main()

輸出:
一共獲取 31 個代理
********************************************************************************
開始創(chuàng)建線程處理.....
正在驗證 : 122.10.48.13
正在驗證 : 122.72.76.121
正在驗證 : 122.72.11.129
正在驗證 : 222.89.159.131
正在驗證 : 218.5.74.174
正在驗證 : 218.203.107.165
正在驗證 : 219.224.101.81
正在驗證 : 221.176.169.14
正在驗證 : 112.5.254.85
正在驗證 : 113.106.73.210
正在驗證 : 114.247.21.212
正在驗證 : 122.72.76.122
正在驗證 : 219.239.26.23
正在驗證 : 222.89.154.14
正在驗證 : 58.67.147.197
正在驗證 : 222.188.88.26
正在驗證 : 103.247.16.241
正在驗證 : 183.221.250.141
正在驗證 : 183.221.250.137
正在驗證 : 122.72.80.108
正在驗證 : 122.72.76.125
正在驗證 : 122.72.11.131
正在驗證 : 122.72.80.101
正在驗證 : 122.72.120.41
正在驗證 : 122.72.120.38
正在驗證 : 122.72.120.35
正在驗證 : 218.203.105.26
正在驗證 : 221.130.18.211
正在驗證 : 110.77.236.48
正在驗證 : 218.91.206.146
正在驗證 : 211.162.16.210
成功采集 114.247.21.212 0.300999879837
成功采集 218.203.105.26 0.306999921799
成功采集 221.176.169.14 0.417000055313
成功采集 122.72.120.35 0.369999885559
采集失敗 218.5.74.174 :timeout
成功采集 122.72.120.38 0.40900015831
成功采集 183.221.250.137 0.608999967575
成功采集 122.72.11.131 0.679999828339
成功采集 183.221.250.141 0.791000127792
成功采集 113.106.73.210 0.891000032425
成功采集 122.72.76.121 1.40299987793
成功采集 122.72.80.108 1.4470000267
成功采集 211.162.16.210 1.625
成功采集 122.72.76.125 1.6819999218
成功采集 112.5.254.85 1.74399995804
成功采集 122.72.80.101 1.79799985886
成功采集 122.72.11.129 2.00900006294
成功采集 122.72.120.41 1.99099993706
采集失敗 222.188.88.26 :timeout
成功采集 122.72.76.122 3.49100017548
成功采集 218.91.206.146 3.66000008583
成功采集 122.10.48.13 3.91799998283
成功采集 222.89.154.14 3.93499994278
成功采集 222.89.159.131 3.99699997902
成功采集 221.130.18.211 3.99500012398
采集失敗 219.224.101.81 :timeout采集失敗 218.203.107.165 :timeout
采集失敗 58.67.147.197 :timeout
采集失敗 103.247.16.241 :timeout
采集失敗 110.77.236.48 :timeout
成功采集 219.239.26.23 12.2809998989
獲取有效代理 24 個,現(xiàn)在開始排序和保存
總共用時 13.2810001373
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 霍山县| 岳阳市| 汾西县| 兰州市| 隆回县| 隆昌县| 枣庄市| 防城港市| 青州市| 洛浦县| 叙永县| 右玉县| 延边| 石台县| 湖南省| 临高县| 百色市| 景德镇市| 黄石市| 诸城市| 海宁市| 石景山区| 米泉市| 江孜县| 铜陵市| 定兴县| 鹤壁市| 体育| 天水市| 波密县| 景泰县| 珲春市| 鄂托克旗| 锦州市| 景谷| 宿迁市| 宜宾县| 许昌市| 鹿邑县| 日喀则市| 湟中县|