Python使用xpath實現(xiàn)圖片爬取
高性能異步爬蟲
目的:在爬蟲中使用異步實現(xiàn)高性能的數(shù)據(jù)爬取操作
異步爬蟲的方式:
- 多線程、多進程(不建議):
好處:可以為相關(guān)阻塞的操作單獨開啟多線程或進程,阻塞操作就可以異步執(zhí)行;
弊端:無法無限制的開啟多線程或多進程。
- 線程池、進程池(適當(dāng)?shù)氖褂茫?br />
好處:我們可以降低系統(tǒng)對進程或線程創(chuàng)建和銷毀的一個頻率,從而很好的降低系統(tǒng)的開銷;
弊端:池中線程或進程的數(shù)據(jù)是有上限的。
代碼如下
# _*_ coding:utf-8 _*_ """ @FileName :6.4k圖片解析爬?。ó惒礁咝阅軠y試).py @CreateTime :2020/8/14 0014 10:01 @Author : Lurker Zhang @E-mail : 289735192@qq.com @Desc. : """ import requests from lxml import etree from setting.config import * import json import os import time from multiprocessing.dummy import Pool def main(): # 圖片采集源地址 # source_url = 'http://pic.netbian.com/4kmeinv/' # temp_url = 'http://pic.netbian.com/4kmeinv/index_{}.html' # source_url = 'http://pic.netbian.com/4kdongman/' # temp_url = 'http://pic.netbian.com/4kdongman/index_{}.html' source_url = 'http://pic.netbian.com/4kmingxing/' temp_url = 'http://pic.netbian.com/4kmingxing/index_{}.html' # 本此采集前多少頁,大于1的整數(shù) page_sum = 136 all_pic_list_url = [] if page_sum == 1: pic_list_url = source_url print('開始下載:' + pic_list_url) all_pic_list_url.append(pic_list_url) else: # 先采集第一頁 pic_list_url = source_url # 調(diào)用采集單頁圖片鏈接的函數(shù) all_pic_list_url.append(pic_list_url) # 再采集第二頁開始后面的頁數(shù) for page_num in range(2, page_sum + 1): pic_list_url = temp_url.format(page_num) all_pic_list_url.append(pic_list_url) # 單頁圖片多線程解析 pool1 = Pool(10) pool1.map(down_pic, all_pic_list_url) print('采集完成,本地成功下載{0}張圖片,失敗{1}張圖片。'.format(total_success, total_fail)) # 存儲已下載文件名列表: with open("../depository/mingxing/pic_name_list.json", 'w', encoding='utf-8') as fp: json.dump(pic_name_list, fp) def down_pic(pic_list_url): print("準(zhǔn)備解析圖片列表頁:",pic_list_url) # 獲取圖片列表頁的網(wǎng)頁數(shù)據(jù) pic_list_page_text = requests.get(url=pic_list_url, headers=headers).text tree_1 = etree.HTML(pic_list_page_text) # 獲取圖片地址列表 pic_show_url_list = tree_1.xpath('//div[@class="slist"]/ul//a/@href') pic_url_list = [get_pic_url('http://pic.netbian.com' + pic_show_url) for pic_show_url in pic_show_url_list] # 開始下載并保存圖片(多線程) pool2 = Pool(5) pool2.map(save_pic, pic_url_list) def save_pic(pic_url): print("準(zhǔn)備下載圖片:",pic_url) global total_success, total_fail, pic_name_list,path picname = get_pic_name(pic_url) if not picname in pic_name_list: # 獲取日期作為保存位置文件夾 pic = requests.get(url=pic_url, headers=headers).content try: with open(path + picname, 'wb') as fp: fp.write(pic) except IOError: print(picname + "保存失敗") total_fail += 1 else: pic_name_list.append(picname) total_success += 1 print("成功保存圖片:{0},共成功采集{1}張。".format(picname, total_success)) else: print("跳過,已下載過圖片:" + picname) total_fail += 1 def get_pic_name(pic_url): return pic_url.split('/')[-1] def get_pic_url(pic_show_url): tree = etree.HTML(requests.get(url=pic_show_url, headers=headers).text) return 'http://pic.netbian.com/' + tree.xpath('//div[@class="photo-pic"]/a/img/@src')[0] if __name__ == '__main__': # 讀入已采集圖片的名稱庫,名稱存在重復(fù)的表示已經(jīng)采集過將跳過不采集 if not os.path.exists('../depository/mingxing/pic_name_list.json'): with open("../depository/mingxing/pic_name_list.json", 'w', encoding="utf-8") as fp: json.dump([], fp) with open("../depository/mingxing/pic_name_list.json", "r", encoding="utf-8") as fp: pic_name_list = json.load(fp) path = '../depository/mingxing/' + time.strftime('%Y%m%d', time.localtime()) + '/' if not os.path.exists(path): os.mkdir(path) # 記錄本次采集圖片的數(shù)量 total_success = 0 total_fail = 0 main()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Python中Sync與Async執(zhí)行速度快慢對比
Python新的版本中支持了async/await語法, 很多文章都在說這種語法的實現(xiàn)代碼會變得很快, 但是這種快是有場景限制的。這篇文章將嘗試簡單的解釋為何Async的代碼在某些場景比Sync的代碼快2023-03-03Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式實例詳解
這篇文章主要介紹了Python函數(shù)可變參數(shù)定義及其參數(shù)傳遞方式,以實例形式較為詳細(xì)的分析了Python函數(shù)參數(shù)的使用技巧,需要的朋友可以參考下2015-05-05Python二叉搜索樹與雙向鏈表轉(zhuǎn)換實現(xiàn)方法
這篇文章主要介紹了Python二叉搜索樹與雙向鏈表轉(zhuǎn)換實現(xiàn)方法,涉及Python二叉搜索樹的定義、實現(xiàn)以及雙向鏈表的轉(zhuǎn)換技巧,需要的朋友可以參考下2016-04-04