python - 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 文件
解决方案
您已将所有物品堆叠在一个中。每个项目都应该是每个键的单个值的字典,而您有一个列表。
尝试类似:
for cat, url in zip(category, category_url):
item = dict(category=cat, category_url=url)
yield item
推荐阅读
- django-rest-framework - 自定义身份验证后端冲突默认后端
- flutter - 如何在正文中添加多个容器
- excel - 在每一列中循环一个宏
- sql - 在 SQL Server 的存储过程中解析 XML 并输出
- azure - Azure ExpressRoute 是否可以连接到不同组织拥有的多个订阅?
- matlab - Matlab代码将转换应用于文件夹中的每个文件
- flutter - 如何获取父级中动画值的子级小部件大小
- javascript - How to make this bezier curve resemble the one in the image
- javascript - How to remove double quotes from JS string?
- r - invalid 'type' (character of argument