1

I am just starting out with Scrapy and I'm trying to rename every image I download with item['title']

Here is my spider:

import scrapy
from botName.items import botName


class botName(scrapy.Spider):
    name = "bot"
    allowed_domains = "example.com"
    start_urls = [
        "http://example.com&pageno=%s"
        % page for page in xrange(1, 3)
        ]

    def parse(self, response):
        for sel in response.xpath('//html'):
            item = myBotItem()
            # I want to name files with the result of this
            item['title'] = sel.xpath('//h5/text()').extract()
            item['image_urls'] = sel.xpath('//img/@data-zoom-image').extract()
            yield item

And my Pipeline class

class myBotPipeline(object):

    def process_item(self, item, spider):
        return item

    def file_path(self, request, response=None, info=None):
        image_guid = request.meta['title'][0]
        log.msg(image_guid, level=log.DEBUG)
        return 'full/%s' % (image_guid)

    def get_media_requests(self, item, info):
        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
        return item
Rami Taibah
  • 183
  • 3
  • 13
  • did you find answer for this? – Rami Sedhom Feb 07 '18 at 22:27
  • @Rami Sedhom i search for something like that...https://stackoverflow.com/questions/51543561/understandin-how-rename-images-scrapy-works if have some answers for that to poit me pls... – MagicHat Jul 26 '18 at 17:44

0 Answers0