如何解决“蜘蛛必须返回Request,BaseItem,dict或None,在其中设置了'set'”

时间:2019-05-16 02:37:33

标签: python web-scraping scrapy

我正在尝试创建一个网络刮板,该刮板可以从网站中检索多个表格,但是以某种方式无法提取它们

我一直在寻找解决方案,但似乎没有什么适合我的需求:(

def parse(self, response):
        tables=[]
        for table in response.xpath('//table'):
            yield {
                tables.append(self.retrieve_table(table))
            }

        tables[0] = self.extract_table_info(tables[0] + tables[1])
        tables[2] = self.extract_table_info(tables[2], 1)
        tables[3] = self.extract_table_info(tables[3], 2)
        tables[4] = self.extract_table_info(tables[4], 3)

        company = { 'info': tables[0],
                    'oscilations': tables[2]['oscilations'],
                    'fundamentals': tables[2]['fundamentals'],
                    'patrimonial_balance_data': tables[3],
                    'demonstrative_results_data': tables[4] }

        import json
        with open('data.json', 'w') as f:
            json.dump(company, f, ensure_ascii=False)

它返回的错误是

ERROR: Spider must return Request, BaseItem, dict or None, got 'set'

1 个答案:

答案 0 :(得分:2)

这是由于以下原因:

for table in response.xpath('//table'):
    yield {
        tables.append(self.retrieve_table(table))
    }

为什么这里需要yield?看来只需追加就可以满足您的目标:

for table in response.xpath('//table'):
    tables.append(self.retrieve_table(table))

甚至只是:

tables = response.xpath('//table')
相关问题