1

我正在尝试使用 python scrapy 从网站源中获取一些图像。

整个事情都很好,除了我的管道中的 process_item 方法没有被访问。

这是我的文件:

设置.py:

BOT_NAME = 'dealspider'
SPIDER_MODULES = ['dealspider.spiders']
NEWSPIDER_MODULE = 'dealspider.spiders'

DEFAULT_ITEM_CLASS = 'dealspider.items.DealspiderItem'

ITEM_PIPELINES = ['scrapy.contrib.pipeline.images.ImagesPipeline', dealspider.ImgPipeline.MyImagesPipeline']

IMAGES_STORE = '/Users/Comp/Desktop/projects/ndailydeals/dimages/full'

图像管道:

class MyImagesPipeline(ImagesPipeline):

    def get_media_requests(self, item, info):
        print "inside get_media_requests"
        for image_url in item['image_urls']:

            yield Request(image_url)

    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
        print "inside item_completed"
        return item



    def process_item(self, item, spider):
        if spider.name == 'SgsnapDeal':
            print "inside process_item"
            # some code not relevant to the qn
            deal = DailyDeals(source_website_url=source_website_url, source_website_logo=source_website_logo, description=description, price=price, url=url, image_urls=image_urls, city=city, currency=currency)
            deal.save()

在运行爬虫时没有得到“内部 process_item”。我也尝试在 scrapy.contrib.pipeline.images.py 文件中添加 process_item 函数,但这也不起作用!

def process_item(self, item, info):
    print "inside process"
    pass

问题:一切正常,下载图像,设置 image_paths 等,我知道 get_media_requests 和 item_completed 在 MyImagesPipeline 中有效,因为一些打印语句,但不是 process_item!任何帮助将非常感激..

编辑:这是其他相关文件:

蜘蛛:

from scrapy.spider import BaseSpider
from dealspider.items import DealspiderItem
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.pipeline.images import ImagesPipeline


class SG_snapDeal_Spider(BaseSpider):
    name = 'SgsnapDeal'
    allowed_domains = ['snapdeal.com']
    start_urls = [
        'http://www.snapdeal.com',
        ]

    def parse(self, response):
        item = DealspiderItem()

        hxs = HtmlXPathSelector(response)
        description = hxs.select('/html/body/div/div/div/div/div/div/div/div/div/a/div/div/text()').extract()  
        price = hxs.select('/html/body/div/div/div/div/div/div/div/div/div/a/div/div/div/span/text()').extract()
        url = hxs.select('/html/body/div/div/div/div/div/div/div/div/div/a/@href').extract()
        image_urls = hxs.select('/html/body/div/div/div/div/div/div/div/div/div/a/div/div/img/@src').extract()

        item['description'] = description
        item['price'] = price
        item['url'] = url
        item['image_urls'] = image_urls
        #works fine!!
        return item

SPIDER = SG_snapDeal_Spider()

项目.py:

from scrapy.item import Item, Field

class DealspiderItem(Item):
    description = Field()
    price = Field()
    url = Field()
    image_urls = Field()
    images = Field()
    image_paths = Field()
4

1 回答 1

1

您需要放入process_item单独的管道,将您的项目保存在数据库中。不在images pipeline.

使单独的管道像

class OtherPipeline(object):
  def process_item(self, item, info):
    print "inside process"
    pass

将其包含pipleline在您的settings文件中

于 2013-01-30T01:49:40.390 回答