无法通过管道以自定义方式重命名下载的图像 [英] Trouble renaming downloaded images in a customized manner through pipelines

查看:26
本文介绍了无法通过管道以自定义方式重命名下载的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python的scrapy模块创建了一个脚本,用于从torrent站点下载和重命名电影图像,并将它们存储在scrapy项目中的文件夹中.当我按原样运行我的脚本时,我发现它可以无误地下载该文件夹文件夹中的图像.

I've created a script using python's scrapy module to download and rename movie images from a torrent site and store them in a folder within scrapy project. When I run my script as it is, I find it downloading images in that folder folder errorlessly.

此时脚本正在使用 request.url 到 pipelines.py 中方便的部分重命名这些图像.

At this moment the script is renaming those images using a convenient portion from request.url through pipelines.py.

我如何通过 pipelines.py 使用变量 movie<中的电影名称重命名这些下载的图像/code> 定义在 get_images() 方法中吗?

How can I rename those downloaded images through pipelines.py using their movie names from the variable movie defined within get_images() method?

蜘蛛包含:

spider contains:

from scrapy.crawler import CrawlerProcess
import scrapy, os

class yify_sp_spider(scrapy.Spider):
    name = "yify"
    start_urls = ["https://yts.am/browse-movies"]

    custom_settings = {
        'ITEM_PIPELINES': {'yify_spider.pipelines.YifySpiderPipeline': 1},
        'IMAGES_STORE': r"C:\Users\WCS\Desktop\yify_spider\yify_spider\spiders\Images",
    }

    def parse(self, response):
        for item in response.css(".browse-movie-wrap"):
            movie_name = ''.join(item.css(".browse-movie-title::text").get().split())
            img_link = item.css("img.img-responsive::attr(src)").get()
            yield scrapy.Request(img_link, callback=self.get_images,meta={'movie':movie_name})

    def get_images(self, response):
        movie = response.meta['movie']
        yield {
            "movie":movie,
            'image_urls': [response.url],
        }

if __name__ == "__main__":
    c = CrawlerProcess({
        'USER_AGENT': 'Mozilla/5.0',   
    })
    c.crawl(yify_sp_spider)
    c.start()

pipelines.py 包含:

pipelines.py contains:

from scrapy.pipelines.images import ImagesPipeline

class YifySpiderPipeline(ImagesPipeline):

    def file_path(self, request, response=None, info=None):
        image_name = request.url.split('/')[-2]+".jpg"
        return image_name

重命名完成后,其中一个下载的图像应该看起来像 Obsession.jpg.

One of such downloaded images should look like Obsession.jpg when the renaming is done.

推荐答案

覆盖 get_media_requests() 并将您需要的数据添加到请求中.然后从 file_path() 中的请求中获取该数据.

Override get_media_requests() and add the data you need to the request. Then grab that data from the request in file_path().

例如:

class YifySpiderPipeline(ImagesPipeline):

    def get_media_requests(self, item, info):
        # Here we add the whole item, but you can add only a single field too.
        return [Request(x, meta={'item': item) for x in item.get(self.images_urls_field, [])]

    def file_path(self, request, response=None, info=None):
        item = request.meta.get('item')
        movie = item['movie']
        # Construct the filename.
        return image_name

这篇关于无法通过管道以自定义方式重命名下载的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆