摘要:簡介用類提供一種方便的方式來下載和存儲圖片。如圖片下載完畢后,處理結果會以二元組的方式返回給函數(shù)。如圖片結果則圖片名稱如果想進行更改,請參考使用框架的下載圖片如何保持原文件名呢
1.ImagesPipeline簡介
Scrapy用ImagesPipeline類提供一種方便的方式來下載和存儲圖片。
特點:
將下載圖片轉換成通用的JPG和RGB格式
避免重復下載
縮略圖生成
圖片大小過濾
當使用圖片管道 ImagePipeline,典型的工作流程如下:
在一個爬蟲里,你抓取一個項目,把其中圖片的URL放入image_urls組內。
項目從爬蟲內返回,進入項目管道。
當項目進入ImagePipeline, image_urls組內的URLs將被Scrapy的調度器和下載器安排下載(這意味著調度器和中間件可以復用),當優(yōu)先級更高,會在其他頁面被抓取前處理. 項目會在這個特定的管道階段保持"locker"的狀態(tài),直到完成圖片的下載(或者由于某些原因未完成下載)。
當圖片下載完, 另一個組(images)將被更新到結構中,這個組將包含一個字典列表,其中包括下載圖片的信息,比如下載路徑,源抓取地址(從image_urls組獲得)和圖片的校驗碼. images列表中的圖片順序將和源image_urls組保持一致.如果某個圖片下載失敗,將會記錄下錯誤信息,圖片也不會出現(xiàn)在images組中。
項目目錄結構:
要想成功爬取圖片,需要經過以下幾個步驟:
(1) 在items.py中添加image_urls、images和image_paths字段,代碼如下:class DoubanImgsItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() image_urls = Field() images = Field() image_paths = Field()(2)在settings.py中設置條件和屬性,代碼如下:
# Configure item pipelines # See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html # ImagePipeline的自定義實現(xiàn)類 ITEM_PIPELINES = { "douban_imgs.pipelines.DoubanImgDownloadPipeline": 300, } #設置圖片下載路徑 IMAGES_STORE = "D:doubanimgs" # 過期天數(shù) IMAGES_EXPIRES = 90 #90天內抓取的都不會被重抓(3)在spiders/download_douban.py中書寫ImageSpider的代碼:
# coding=utf-8 from scrapy.spiders import Spider import re from scrapy import Request from ..items import DoubanImgsItem class download_douban(Spider): name = "download_douban" default_headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, sdch, br", "Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6", "Cache-Control": "max-age=0", "Connection": "keep-alive", "Host": "www.douban.com", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36", } def __init__(self, url="1638835355", *args, **kwargs): self.allowed_domains = ["douban.com"] self.start_urls = [ "http://www.douban.com/photos/album/%s/" % (url)] self.url = url # call the father base function # super(download_douban, self).__init__(*args, **kwargs) def start_requests(self): for url in self.start_urls: yield Request(url=url, headers=self.default_headers, callback=self.parse) def parse(self, response): list_imgs = response.xpath("http://div[@class="photolst clearfix"]//img/@src").extract() if list_imgs: item = DoubanImgsItem() item["image_urls"] = list_imgs yield item(4)在pipelines.py中自定義ImagePipeline代碼:
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don"t forget to add your pipeline to the ITEM_PIPELINES setting # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html from scrapy.pipelines.images import ImagesPipeline from scrapy.exceptions import DropItem from scrapy import Request from scrapy import log class DoubanImgsPipeline(object): def process_item(self, item, spider): return item class DoubanImgDownloadPipeline(ImagesPipeline): default_headers = { "accept": "image/webp,image/*,*/*;q=0.8", "accept-encoding": "gzip, deflate, sdch, br", "accept-language": "zh-CN,zh;q=0.8,en;q=0.6", "cookie": "bid=yQdC/AzTaCw", "referer": "https://www.douban.com/photos/photo/2370443040/", "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36", } def get_media_requests(self, item, info): for image_url in item["image_urls"]: self.default_headers["referer"] = image_url yield Request(image_url, headers=self.default_headers) def item_completed(self, results, item, info): image_paths = [x["path"] for ok, x in results if ok] if not image_paths: raise DropItem("Item contains no images") item["image_paths"] = image_paths return item
在自定義ImagePipeline代碼中,作為重要的是要重載get_media_requests(self, item, info)和item_completed(self, results, item, info)這兩個函數(shù)。
get_media_requests(self,item, info):
ImagePipeline根據(jù)image_urls中指定的url進行爬取,可以通過get_media_requests為每個url生成一個Request。如:
for image_url in item["image_urls"]: self.default_headers["referer"] = image_url yield Request(image_url, headers=self.default_headers)
item_completed(self, results, item, info):
圖片下載完畢后,處理結果會以二元組的方式返回給item_completed()函數(shù)。這個二元組定義如下:
(success, image_info_or_failure)
其中,第一個元素表示圖片是否下載成功;第二個元素是一個字典。如:
def item_completed(self, results, item, info): image_paths = [x["path"] for ok, x in results if ok] if not image_paths: raise DropItem("Item contains no images") item["image_paths"] = image_paths return item
運行結果如下:
下載成功以后,你就會在剛才設置的保存圖片的路徑里看到下載完成的圖片:IMAGES_STORE = "D:doubanimgs"
默認情況下,使用ImagePipeline組件下載圖片的時候,圖片名稱是以圖片URL的SHA1值進行保存的。
如:
圖片URL:http://www.example.com/image.jpg
SHA1結果:3afec3b4765f8f0a07b78f98c07b83f013567a0a
則圖片名稱:3afec3b4765f8f0a07b78f98c07b83f013567a0a.jpg
如果想進行更改,請參考:使用scrapy框架的ImagesPipeline下載圖片如何保持原文件名呢?
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://m.hztianpu.com/yun/44408.html
摘要:百度云搜索,搜各種資料搜網盤,搜各種資料注意數(shù)據(jù)保存的操作都是在文件里操作的將數(shù)據(jù)保存為文件是一個信號檢測導入圖片下載器模塊定義數(shù)據(jù)處理類,必須繼承初始化時打開文件為數(shù)據(jù)處理函數(shù),接收一個,里就是爬蟲最后來的數(shù)據(jù)對象文章標題是 【百度云搜索,搜各種資料:http://www.bdyss.cn】 【搜網盤,搜各種資料:http://www.swpan.cn】 注意:數(shù)據(jù)保存的操作都是在p...
摘要:當項目進入,組內的將被的調度器和下載器這意味著調度器和下載器的中間件可以復用安排下載,當優(yōu)先級更高,會在其他頁面被抓取前處理。這個組將包含一個字典列表,其中包括下載文件的信息,比如下載路徑源抓取地址從組獲得和圖片的校驗碼。 1. 最常見爬取圖片方法 對于圖片爬取,最容易想到的是通過urllib庫或者requests庫實現(xiàn)。具體兩種方法的實現(xiàn)如下: 1.1 urllib 使用urllib...
摘要:百度云搜索,搜各種資料搜網盤,搜各種資料編寫爬蟲文件循環(huán)抓取內容方法,將指定的地址添加到下載器下載頁面,兩個必須參數(shù),參數(shù)頁面處理函數(shù)使用時需要方法,是庫下的方法,是自動拼接,如果第二個參數(shù)的地址是相對路徑會自動與第一個參數(shù)拼接導 【百度云搜索,搜各種資料:http://bdy.lqkweb.com】 【搜網盤,搜各種資料:http://www.swpan.cn】 編寫spiders爬...
摘要:很多人學習爬蟲的第一驅動力就是爬取各大網站的妹子圖片,比如比較有名的。最后我們只需要運行程序,即可執(zhí)行爬取,程序運行命名如下完整代碼我已上傳到微信公眾號后臺,在癡海公眾號后臺回復即可獲取。本文首發(fā)于公眾號癡海,后臺回復即可獲取最新編程資源。 showImg(https://segmentfault.com/img/remote/1460000016780800); 閱讀文本大概需要 1...
摘要:爬取百思不得姐首先一步一步來,我們先從爬最簡單的文本開始。將百思不得姐段子保存到中別忘了將管道加到配置文件中。雖然我只是簡單的爬了百思不得姐,不過這些方法可以應用到其他方面,爬取更多更有用的數(shù)據(jù)。 前一篇文章介紹了很多關于scrapy的進階知識,不過說歸說,只有在實際應用中才能真正用到這些知識。所以這篇文章就來嘗試利用scrapy爬取各種網站的數(shù)據(jù)。 爬取百思不得姐 首先一步一步來,我...
閱讀 4025·2021-10-19 13:23
閱讀 2403·2021-09-09 11:37
閱讀 2599·2019-08-29 15:20
閱讀 3505·2019-08-29 11:08
閱讀 1764·2019-08-26 18:27
閱讀 1823·2019-08-23 12:20
閱讀 3115·2019-08-23 11:54
閱讀 2661·2019-08-22 15:19