scrapy如何导出项目以单独的csv文件 [英] How can scrapy export items to separate csv files per item

查看:623
本文介绍了scrapy如何导出项目以单独的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在追踪一个足球网站,蜘蛛(一个蜘蛛)从网站的页面中获得几种项目:团队,比赛,俱乐部等
我试图使用CSVItemExporter来存储这些项目单独的csv文件,
teams.csv,matches.csv,clubs.csv等。

I am scraping a soccer site and the spider (a single spider) gets several kinds of items from the site's pages: Team, Match, Club etc. I am trying to use the CSVItemExporter to store these items in separate csv files, teams.csv, matches.csv, clubs.csv etc.

我不知道什么是正确的方法。
到目前为止,我唯一的方法是创建我自己的自定义管道,例如
http://doc.scrapy.org/en/0.14/topics/exporters.html ,并在spider_opened方法中打开所有需要的csv文件,即为每个文件创建一个csv导出器csv文件,并在process_item放置代码中找出什么样的项目是item参数,然后将其发送到相应的导出器对象。

I am not sure what is the right way to do this. The only way I have thought so far is to create my own custom pipeline like in the example http://doc.scrapy.org/en/0.14/topics/exporters.html and there open all needed csv files in the spider_opened method, ie create a csv exporter for each csv file and in the process_item put code to figure out what kind of item is the "item" parameter and then send it to the corresponding exporter object.

无论如何,我没有找到任何处理多个csv文件(每个项目类型)在scrapy的示例,所以我担心我使用它的方式不是意味着要使用。 (这是我第一次使用Scrapy)。

Anyway I haven't found any examples of handling multiple csv files (per item type) in scrapy so I am worrying that I am using it in a way that is not meant to be used. (this is my first experience with Scrapy).

diomedes

推荐答案

你的做法似乎对我很好。

You approach seems fine to me. Piplines are a great feature of Scrapy and are IMO build for something like your approach.

您可以创建多个项目(例如SoccerItem,MatchItem),并在 MultiCSVItemPipeline 只需通过检查项目类将每个项目委托给自己的CSV类。

You could create multiple items (e.g. SoccerItem, MatchItem) and in your MultiCSVItemPipeline just delegate each item to its own CSV class by checking the item class.

这篇关于scrapy如何导出项目以单独的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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