Scrapy下載 圖片

直接複製到pipelines.py

import scrapy
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline
class PicsDownloadPipeline(ImagesPipeline):

    def get_media_requests(self, item, info):

            yield scrapy.Request(item['goods_img'])

    def item_completed(self, results, item, info):
        # 將下載的圖片路徑(傳入到results中)存儲到 image_paths 項目組中,如果其中沒有圖片,我們將丟棄項目:
        image_path = [x['path'] for ok, x in results if ok]
        if not image_path:
            raise DropItem("Item contains no images")
        item['goods_img'] = image_path
        return item

更改settings.py

ITEM_PIPELINES = {
   'snacks.pipelines.MyProjectPipeline': 300,
   # 'scrapy.pipelines.images.ImagesPipeline':5,
}



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