'How to send data from one pipeline to another in Scrapy
Hello I have two pipelines, the first one to download photos:
class ModelsPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for image_url in item['image_urls']:
yield scrapy.Request(image_url)
def file_path(self, request, response=None, info=None, *, item=None):
image_url_hash = hashlib.shake_256(request.url.encode()).hexdigest(5)
image_filename = f'{item["name"]}/{image_url_hash}.jpg'
return image_filename
def item_completed(self, results, item, info):
image_paths = [x['path'] for ok, x in results if ok]
for image in image_paths:
file_extension = os.path.splitext(image)[1]
img_path = f'{IMAGES_STORE}{image}'
md5 = hashlib.md5(open(img_path, 'rb').read()).hexdigest()
img_destination = f'{IMAGES_STORE}{item["name"]}/{md5}{file_extension}'
os.rename(img_path, img_destination)
return item
The second one is to store previous info in the database
class DatabasePipeline():
def open_spider(self, spider):
self.client = db_connect()
def close_spider(self, spider):
self.client.close()
def process_item(self, item, spider):
self.client.upsert(item)
The item_completed function in the first pipeline, returns a name and a path that I want to send to the second pipeline in order to store in the database, but I can not get access to that data.
The question is how can I do that?
Thanks
Solution 1:[1]
You can add the name and the path to the item in the ModelsPipeline:
item['name_from_pipeline'] = name
item['path_from_pipeline'] = path
return item
In process_item of the DatabasePipeline you can access it:
name = item['name_from_pipeline']
path = item['path_from_pipeline']
Solution 2:[2]
First index is always included, end index excluded.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wim Hermans |
| Solution 2 | Mojtaba Abdi Kh. |
