【发布时间】:2019-02-17 20:37:53
【问题描述】:
我使用 python 的 scrapy 模块创建了一个脚本,用于从 torrent 站点的多个页面下载和重命名电影图像,并将它们存储在桌面文件夹中。当它是关于下载这些图像并将其存储在桌面文件夹中时,我的脚本是相同的,没有错误。但是,我现在正在努力做的是即时重命名这些文件。由于我没有使用item.py文件,我也不想使用,我几乎不明白pipelines.py文件的逻辑是如何处理重命名过程的。
我的蜘蛛 (It downloads the images flawlessly):
from scrapy.crawler import CrawlerProcess
import scrapy, os
class YifySpider(scrapy.Spider):
name = "yify"
allowed_domains = ["www.yify-torrent.org"]
start_urls = ["https://www.yify-torrent.org/search/1080p/p-{}/".format(page) for page in range(1,5)]
custom_settings = {
'ITEM_PIPELINES': {'scrapy.pipelines.images.ImagesPipeline': 1},
'IMAGES_STORE': r"C:\Users\WCS\Desktop\Images",
}
def parse(self, response):
for link in response.css("article.img-item .poster-thumb::attr(src)").extract():
img_link = response.urljoin(link)
yield scrapy.Request(img_link, callback=self.get_images)
def get_images(self, response):
yield {
'image_urls': [response.url],
}
if __name__ == "__main__":
c = CrawlerProcess({
'USER_AGENT': 'Mozilla/5.0',
})
c.crawl(YifySpider)
c.start()
pipelines.py 包含:(the following lines are the placeholders to let you know I at least tried):
from scrapy.http import Request
class YifyPipeline(object):
def file_path(self, request, response=None, info=None):
image_name = request.url.split('/')[-1]
return image_name
def get_media_requests(self, item, info):
yield Request(item['image_urls'][0], meta=item)
如何在不使用item.py 的情况下通过pipelines.py 重命名图像?
【问题讨论】:
-
我不明白
item.py与它有什么关系。你会如何使用它?
标签: python python-3.x web-scraping scrapy scrapy-spider