2013-04-16 107 views
7

我想通過scrapy下載圖像。這裏是我的不同的文件:在scrapy中下載圖像

items.py

class DmozItem(Item): 
     title = Field() 
     image_urls = Field() 
     images = Field() 

settings.py

BOT_NAME = 'tutorial' 

SPIDER_MODULES = ['tutorial.spiders'] 
NEWSPIDER_MODULE = 'tutorial.spiders' 
ITEM_PIPELINES = ['scrapy.contrib.pipeline.images.ImagesPipeline'] 
IMAGES= '/home/mayank/Desktop/sc/tutorial/tutorial' 

蜘蛛

class DmozSpider(BaseSpider): 
    name = "wikipedia" 
    allowed_domains = ["wikipedia.org"] 
    start_urls = [ 
     "http://en.wikipedia.org/wiki/Pune" 
    ] 

    def parse(self, response): 
     hxs = HtmlXPathSelector(response) 
     items = [] 
     images=hxs.select('//a[@class="image"]') 
     for image in images: 
       item = DmozItem() 
       link=image.select('@href').extract()[0] 
       link = 'http://en.wikipedia.com'+link 
       item['image_urls']=link 
       items.append(item) 

儘管所有這些設置我在我的流水線沒有得到激活。請幫助。我是這個框架的新手。

+0

您是否安裝了PIL(Python Imaging Library)?這是圖像下載的先決條件:http://doc.scrapy.org/en/latest/topics/images.html – Talvalin

+0

你怎麼知道管道沒有被激活?你可以包含一些日誌輸出,如:'2013-04-16 16:40:31-0500 [scrapy] DEBUG:啓用項目管道:ImagesPipeline'。 –

回答

10

首先,settings.py:圖像 - > IMAGES_STORE

二,spider:你應該返回一個item使ImagesPipeline可以下載這些圖片。

item = DmozItem() 
image_urls = hxs.select('//img/@src').extract() 
item['image_urls'] = ["http:" + x for x in image_urls] 
return item