如何使用Scrapy下载图片?

How to download image using Scrapy?

我是 scrapy 的新手。我正在尝试从 here. I was following Official-Doc and this article 下载图像。

我的 settings.py 看起来像:

BOT_NAME = 'shopclues'

SPIDER_MODULES = ['shopclues.spiders']
NEWSPIDER_MODULE = 'shopclues.spiders'

ROBOTSTXT_OBEY = True

ITEM_PIPELINES = {
    'scrapy.contrib.pipeline.images.ImagesPipeline':1
}

IMAGES_STORE="home/pr.singh/Projects"


items.py 看起来像:

import scrapy
from scrapy.item import Item

class ShopcluesItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass

class ImgData(Item):
    image_urls=scrapy.Field()
    images=scrapy.Field()

我觉得这两个文件都不错。但是我无法编写正确的蜘蛛来获取图像。我可以抓取图像 URL 但不知道如何使用 imagePipeline.
存储图像 我的蜘蛛看起来像:

from shopclues.items import ImgData
import scrapy
import datetime


class DownloadFirstImg(scrapy.Spider):
    name="DownloadfirstImg"
    start_urls=[
    'http://www.shopclues.com/canon-powershot-sx410-is-2.html',
    ]

    def parse (self, response):
        url= response.css("body div.site-container div#container div.ml_containermain div.content-helper div.aside-site-content div.product form#product_form_83013851 div.product-gallery div#product_images_83013851_update div.slide a#det_img_link_83013851_25781870")

        yield scrapy.Request(url.xpath('@href').extract(),self.parse_page)

        def parse_page(self,response):
            imgURl=response.css("body div.site-container div#container div.ml_containermain div.content-helper div.aside-site-content div.product form#product_form_83013851 div.product-gallery div#product_images_83013851_update div.slide a#det_img_link_83013851_25781870::attr(href)").extract()

            yield {
            ImgData(image_urls=[imgURl])
            }

我在this-article之后写了蜘蛛。但我什么也没得到。我 运行 我的蜘蛛是 scrapy crawl DownloadfirstImg -o img5.json 但我没有得到任何 json 或任何图像?
关于如何抓取图像的任何帮助 url 已知.我也从未与 python 合作过,所以事情对我来说似乎很复杂。指向任何好的教程的链接可能会有所帮助。 TIA

我不明白你为什么要生成图像请求,你只需要将它保存在项目上,图像管道将完成剩下的工作,这就是你所需要的。

def parse (self, response):
    url= response.css("body div.site-container div#container div.ml_containermain div.content-helper div.aside-site-content div.product form#product_form_83013851 div.product-gallery div#product_images_83013851_update div.slide a#det_img_link_83013851_25781870")
    yield ImgData(image_urls=[url.xpath('@href').extract_first()])