首页 > 解决方案 > Scrapy + python:csv文件未按正确顺序导出

问题描述

我正在用我的蜘蛛创建一个 csv 文件,但它给了我一个奇怪的数据顺序:

我的代码:

class GoodmanSpider(scrapy.Spider):
name = "goodmans"
start_urls = ['http://www.goodmans.net/d/1706/brands.htm']

def parse(self, response):
    items = TutorialItem()
    all_data = response.css('.SubDepartments')
    for data in all_data:
        category = data.css('.SubDepartments a::text').extract()
        category_url = data.css('.SubDepartments a::attr(href)').extract()
        items['category'] = category
        items['category_url'] = category_url
        yield items

我的 items.py 文件

我的 items.py 文件

我得到的输出: 我得到的输出

我想要的输出,或多或少: 我想要的输出,或多或少

标签: pythonscrapy

解决方案


您已将所有物品堆叠在一个中。每个项目都应该是每个键的单个值的字典,而您有一个列表。

尝试类似:

for cat, url in zip(category, category_url):
    item = dict(category=cat, category_url=url)
    yield item

推荐阅读