【问题标题】:Trouble writing to csv file with scrapy spider使用爬虫蜘蛛写入 csv 文件时遇到问题
【发布时间】:2019-01-10 09:30:22
【问题描述】:

我写了一个简单的蜘蛛,当我尝试将数据写入 csv 文件时出现错误。我得到的错误 ERROR: Spider must return Request, BaseItem, dict or None, got 'str' 当我尝试打印结果时,一切似乎都很好,我只是不知道如何将其写入 csv。我使用了scrapy crawl mmadness -o file.csv,但除了那个错误之外什么都没有发生。 CSV 文件已创建,但它是空的。 问题是我不确定我应该如何处理最后一条语句或如何正确返回“table_rows”。 谢谢

# -*- coding: utf-8 -*-
import scrapy


class MmadnessSpider(scrapy.Spider):
    name = 'mmadness'
    allowed_domains = ['ncaa.com/standings/basketball-men/d1']
    start_urls = ['http://ncaa.com/standings/basketball-men/d1/']

    def parse(self, response):
        rows = response.css('tbody tr')
        for row in rows:
            table_rows = row.css('td::text').extract()
            print(table_rows)

【问题讨论】:

    标签: python-3.x scrapy


    【解决方案1】:
    print(table_rows)
    

    您正在打印您应该使用的数据:

    yield(table_rows)
    

    【讨论】:

    • yield 给我 en 错误:错误:Spider 必须返回 Request、BaseItem、dict 或 None,得到 'list'
    • 是的。它的基本结构,使用`yield {'table_row': table_row}
    • 永远快乐。
    猜你喜欢
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2012-04-06
    • 2013-11-30
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多