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.
At this moment the script is renaming those images using a convenient portion from request.url through pipelines.py
.
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 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
One of such downloaded images should look like Obsession.jpg
when the renaming is done.