闲来无事,看看python futures 多线程这块,确实方便 牛皮
from concurrent.futures import ThreadPoolExecutor import requests import random import pytesseract from PIL import Image url = ["http://www.epicc.com.cn/wap/CreateImage?randName=verifypicForCar&next=%d"%x for x in range(10)] h = { "Referer":"http://www.epicc.com.cn/wap/views/policyClaim/index.jsp", "User-Agent":"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Mobile Safari/537.36", } def aaa(url): filename = str(random.random()).split('.')[-1] info = requests.get(url,headers=h).content f=open(filename+".png","wb") f.write(info) f.close() return filename def shibie(res): res = res.result() image = Image.open(res+".png") code = pytesseract.image_to_string(image) f = open('1.txt',"a+") f.write(code+"\n") f.close() ex = ThreadPoolExecutor(max_workers=10) for i in url: ex.submit(aaa,i).add_done_callback(shibie)
随手写的 很low
url = ["http://www.epicc.com.cn/wap/CreateImage?randName=verifypicForCar&next=%d"%x for x in range(1)] h = { "Referer":"http://www.epicc.com.cn/wap/views/policyClaim/index.jsp", "User-Agent":"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Mobile Safari/537.36", } filename = str(random.random()).split('.')[-1] def aaa(url): info = requests.get(url,headers=h).content f=open("ttt/"+filename+".png","wb") f.write(info) f.close() return "OK" s = time.time() ex = ThreadPoolExecutor(max_workers=10) res = ex.map(aaa,url) print list(res) e = time.time() print "多线程用时",e-s
常用的 这俩方法 根据应用场景不同自行选择