菜鸟Q关于瘙痒管道.py

2024-10-04 11:25:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习那篇无聊的教程。为了测试这个过程,我用这些文件创建了一个新项目:

See my post in Scrapy group for links to scripts, I cannot post more than 1 link here.

spider运行良好,在title标签之间刮取文本并将其放入FirmItem中

[whitecase.com] INFO: Passed FirmItem(title=[u'White & Case LLP - Lawyers - Rachel B. Wagner ']) 

但我被困在了流水线上。我想把这个FirmItem添加到一个csv文件中,这样我就可以把它添加到数据库中。在

我是python的新手,我在不断学习。如果有人能告诉我如何制作管道.py将收集到的数据放入项目.csv. 在

谢谢。在


Tags: csvto项目infortitle过程my
3条回答

将内置的CSV feed export(在v0.10中可用)与CsvItemExporter一起使用。在

我想他们在Scrapy Tutorial中提到了你的具体问题。在

它建议,就像其他人使用CSV模块一样。将以下内容放入pipelines.py文件中。在

import csv

class CsvWriterPipeline(object):

    def __init__(self):
        self.csvwriter = csv.writer(open('items.csv', 'wb'))

    def process_item(self, domain, item):
        self.csvwriter.writerow([item['title'][0], item['link'][0], item['desc'][0]])
        return item

Don’t forget to enable the pipeline by adding it to the ITEM_PIPELINES setting in your settings.py, like this:

^{pr2}$

调整以适应项目的具体情况。在

Python有一个module for reading/writing CSV files,这比自己编写输出更安全(并且正确地引用/转义…)

import csv
csvfile = csv.writer(open('items.csv', 'w'))
csvfile.writerow([ firmitem.title, firmitem.url ])
csvfile.close()

相关问题 更多 >