Scrapy基礎————圖片下載後將本地路徑添加到Item中
阿新 • • 發佈:2017-05-22
ica cal 行處理 pipe 思路 completed div self 狀態
前邊講到簡單的圖片下載,但是怎樣將圖片的本地路徑和存儲在Item中的數據對應起來,所以本篇博文講解到如何將
本地的下載後的圖片路徑寫入到Item中
思路:自定義pipline,多加個管道,該管道繼承下載圖片的類,並重寫與Item 交互的方法,從眾提取到本地路徑,並返回這個Item交給下一個pipline管道
具體代碼:
先導入
from scrapy.pipelines.images import ImagesPipeline
1 #補充Item的字段,將文章列表頁的圖片下載下來,並將圖片的路徑添加到item 2 class ArticalImagesPipeline(ImagesPipeline):3 def item_completed(self, results, item, info): 4 for ok,value in results: #debuge得知:result是一個元組,(狀態,{"path":"","url":"http://***"}) 5 image_file_path = value["path"] 6 item["front_image_path"] = image_file_path #將提取到的path寫入到Item中 7 returnitem #將加工後的Item傳遞給下一個管道進行處理
將自定義的管道加入到setting的管道管理的設置
Scrapy基礎————圖片下載後將本地路徑添加到Item中