python併發爬蟲實用工具tomorrow實用解析

這篇文章主要介紹了python併發爬蟲實用工具tomorrow實用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考
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,就要儘量減少耗時操作,訪問網絡並等待其迴應就是一個非常耗時的工作,在這裏我們需要做的是,併發的時候除了訪問網絡不要做其他操作,然後我們把獲取的請求存一個列表,然後再去循環做其他操作,看不懂我說的沒關係,直接看下面代碼並嘗試幾次就明白了。
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",
      "http://film.spider.com.cn",
      "https://dianying.taobao.com/",
      "http://www.wandafilm.com/",
      "http://www.dygang.net/",
      "http://www.bale.cn/",
      "http://dianying.2345.com/",
      "http://v.x2y4.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就好.
寫到這裏,給大家推薦一個資源很全的python學習聚集地,點擊進入,這裏有資深程序員分享以前學習心得,學習筆記,還有一線企業的工作經驗,且給大家精心整理一份python零基礎到項目實戰的資料,每天給大家講解python最新的技術,前景,學習需要留言的小細節

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章