前言
本文的文字及圖片過濾網路,可以學習,交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
以下文章來源于青燈編程 ,作者:清風
如上圖所示,爬取171個視頻,共計2.6G的記憶體大小,用時僅有88秒,還不到一分半,
基本開發環境
- Python 3.6
- 皮查姆
相關模塊的使用
import re import time import requests import concurrent.futures
相關模塊pip安裝即可,
完整代碼
import re import time import requests import concurrent.futures def get_response(html_url): headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'} response = requests.get(url=html_url, headers=headers) return response def save(video_url, video_title): filename = 'video\\' + video_title + '.mp4' video_data = get_response(video_url).content with open(filename, mode='wb') as f: f.write(video_data) print('正在保存:', video_title) def main(html_url): html_data = get_response(html_url).text lis = re.findall('<div id="(\d+)" >', html_data) for li in lis: page_url = f'https://www.thepaper.cn/newsDetail_forward_{li}' page_data = get_response(page_url).text video_url = re.findall('<source src="https://www.cnblogs.com/hhh188764/p/(.*?)" type="video/mp4"/>', page_data)[0] video_title = re.findall('<h2>(.*?)</h2>', page_data)[0] save(video_url, video_title) end_time = time.time() use_time = end_time - start_time print('總共耗時:', use_time) if __name__ == '__main__': start_time = time.time() executor = concurrent.futures.ThreadPoolExecutor(max_workers=5) for page in range(1, 11): url = f'https://www.thepaper.cn/load_video_chosen.jsp?channelID=26916&pageidx={page}' executor.submit(main, url) executor.shutdown()
完整代碼已經給了,自己的感受?,我這還是使用的5個執行緒,你給10個執行緒效率會更高,可以一分鐘不到就可以爬完了
Python爬蟲、資料分析、網站開發等案例教程視頻免費在線觀看
https://space.bilibili.com/523606542
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/243125.html
標籤:Python
上一篇:關于資料抓取很多新人的誤區
