tomorrow是我最近在用的一個爬蟲利器,該模塊屬於第三方的模塊,使用起來非常的方便,只需要用其中的threads方法作為裝飾器去修飾一個普通的函數,既可以達到並發的效果,本篇將用實例來展示tomorrow的強大之處。后面文章將對tomorrow的實現原理做進一步的分析。
1.安裝第三方包
pip install requests_html #網絡請求包 pip install fake-useragent #獲取useragent包 pip install tomorrow
2.普通下載方式
在這里我們用20個電影網址進行測試,並獲取其標題,計算所用的時間
start=time.time() for i in url_list: print(get_xpath(get_req(i),"//title//text()")) end=time.time() print("普通方式花費時間",end-start)
get_req是我定義的訪問網絡的一個方法,get_xpath是為例使用xpath表達式獲取其結果,這里是獲取網址的標題。20個電影網址普通方式訪問的結果在8-9秒之間。
3使用tomorrow以后
start2 = time.time() req_list = [] for url in url_list: req = async_get_req(url) req_list.append(req) for req in req_list: print(get_xpath(req, "//title//text()")) end2 = time.time() print("並發后花費時間", end2 - start2)
如果我們想要使用tomorrow,就要盡量減少耗時操作,訪問網絡並等待其回應就是一個非常耗時的工作,在這里我們需要做的是,並發的時候除了訪問網絡不要做其他操作,然后我們把獲取的請求存一個列表,然后再去循環做其他操作,看不懂我說的沒關系,直接看下面代碼並嘗試幾次就明白了。用時為2s-3s
4.測試結果對比 來看完整代碼
import time from requests_html import HTMLSession from fake_useragent import UserAgent as ua from tomorrow import threads headers = {"User-Agent": ua().Chrome} session = HTMLSession() url_list = ["https://movie.douban.com", "http://www.1905.com/", "http://www.mtime.com/", "https://www.dy2018.com/", "http://dytt8.net", "https://www.piaohua.com/", "http://maoyan.com", "https://www.xigua110.com/", "https://www.vmovier.com/", "http://movie.kankan.com/", "https://107cine.com/", "http://movie.youku.com", "http://film.qq.com","https://dianying.taobao.com/", "http://www.wandafilm.com/", "http://www.dygang.net/","http://dianying.2345.com/", ] def get_req(url, timeout=10): req = session.get(url, headers=headers, timeout=timeout) if req.status_code == 200: return req @threads(5) def async_get_req(url, timeout=10): req = session.get(url, headers=headers, timeout=timeout) if req.status_code == 200: return req def get_xpath(req, xpath_str): return req.html.xpath(xpath_str)[0].strip().replace("\n", "") start=time.time() for i in url_list: print(get_xpath(get_req(i),"//title//text()")) end=time.time() print("普通方式花費時間",end-start) start2 = time.time() req_list = [] for url in url_list: req = async_get_req(url) req_list.append(req) for req in req_list: print(get_xpath(req, "//title//text()")) end2 = time.time() print("並發后花費時間", end2 - start2)
運行三次上面的程序記錄下每次的結果
第一次: 普通方式花費時間 7.883908271789551 並發后花費時間 2.2888755798339844 第二次: 普通方式花費時間 8.522203207015991 並發后花費時間 2.4674007892608643 第三次: 普通方式花費時間 9.062756061553955 並發后花費時間 2.8703203201293945
tomorrow使用起來很簡單,在普通的函數上面加個threads裝飾器即可以實現並發效果, 括號中的數字是表示並發的次數,經過我的測試並不是並發次數越多越好,你需要選擇一個中間點,因為還會受到網速的影響,我覺得一般並發數5-10就好.