Scrapy 将一个 CSV 中的所有数据导出到 AWS S3,而不是多个 CSV

问题描述 投票:0回答:1

我正在尝试使用 MultiCSVItemPipeline 将两个 csv 中的 Scrapy 抓取数据导出到 S3。它在我的计算机上运行良好,数据以两个单独的 csv 导出,但导出到 S3 时,所有数据都以一个 csv 导出到 S3,在我的计算机上以两个 csv 导出。

管道.py


from itemadapter import ItemAdapter
from scrapy.exporters import CsvItemExporter
import csv


class MultiCSVItemPipeline(object):
    file_names = ['results','meta']
    
    
    def open_spider(self, spider):
        self.files = dict([ (name, open(name+'.csv','w+b')) for name in self.file_names ])
        self.exporters = dict([ (name,CsvItemExporter(self.files[name])) for name in self.file_names])
        
        [e.start_exporting() for e in self.exporters.values()]


    def close_spider(self, spider):
        [e.finish_exporting() for e in self.exporter.values()]
        [f.close() for f in self.files.values()]

    def process_item(self, item, spider):
        

        item_1 = {
            'source_id': item.get('source_id'),
            'task_id': item.get('task_id'),
            'job_id':    item.get('job_id'),
            'date_posted': item.get('date_posted'),
            'timestamp': item.get('timestamp'),
            'company': item.get('company'),
            'company_id_1': item.get('company_id_1'),
            'company_id_2': item.get('company_id_2'),
            'location': item.get('location'),
            'title': item.get('title'),
            'summary': item.get('summary'),
            'full_listing_url': item.get('full_listing_url'),
            'task_url': item.get('task_url'),
            'page_url': item.get('link'),
            'timestamp': item.get('time_n')
                      

        }

        item_2 = {
                'source_id': item.get('source_id'),
                'task_id' : item.get('task_id'),
                'job_id': item.get('job_id'),
                'meta_name': item.get('meta_name'),
                'meta_value': item.get('meta_value'),
                'meta_source': item.get('meta_source'),
                'task_url': item.get('task_url'),
                'task_config': item.get('task_config'),
                'timestamp': item.get('time_n')
                
        }

               
        if item.get('meta_source') is not None:
        
            self.exporters['meta'].export_item(item_2)
        else:
            self.exporters['results'].export_item(item_1)
        return item ```


settings.py


```AWS_ACCESS_KEY_ID = <My_access_key>
   AWS_SECRET_ACCESS_KEY = <My_secret_access_key>



FEEDS = {
    's3://indeedusap/results.csv': {
                                        'format': 'csv',
                                       
                                        },
    's3://indeedusap/meta.csv': {
                                        'format': 'csv',
                                       
                                        },
    
}

我尝试将 s3 路径添加到管道中的文件名称中,但它不起作用,我收到 KeyError: 's3://indeedusap/meta'。

python csv amazon-s3 scrapy feed
1个回答
0
投票

我想将我抓取的页面存储在单独的文件中,我尝试了几次但失败了,并且网上没有其他教程。这是一个与Scrapy如何处理上传到s3的文件有关的问题吗?我认为它会将爬行的文件存储在其他地方,然后在蜘蛛关闭时将所有文件上传到 s3。

© www.soinside.com 2019 - 2024. All rights reserved.