简体   繁体   English

如何格式化Scrapy导出

[英]How to format Scrapy exports

I currently store my scraped data in an item to export in .csv format: 我目前将我的抓取数据存储在一个项目中,以.csv格式导出:

item = HobbyItem()
item['name'] = user.getName()
item['hobbies'] = user.getHobbies()

The item is declared as follows: 该项目声明如下:

name = scrapy.Field()
hobbies = scrapy.Field()

This results in a csv format as follows: 结果是csv格式,如下所示:

name,hobbies
Tim, [['tennis'],['squash'],['music']]
Bob, [['rugby'],['polo']]

What I actually want is this: 我真正想要的是:

name,hobbies
Tim, 'tennis'
Tim, 'squash'
Tim, 'music'
Bob, 'rugby'
Bob, 'polo'

Does anybody know how I can modify the output to achieve this? 有谁知道我可以如何修改输出来实现这一目标?

You did not let enough code or explanation for me to be sure that this will work in your particular code but 您没有为我提供足够的代码或说明,以确保这可以在您的特定代码中使用,但

Try : 尝试:

for element in user.getHobbies():
    item['name'] = user.getName()
    item['hobbies'] = element[0]

This will create 1 element per hobby, using getName multiple times 这将为每个爱好创建1个元素,并多次使用getName

name = user.getName()
for hobby in user.getHobbies():
    if hobby:
        item = HobbyItem()
        item['name'] = name
        item['hobbies'] = hobby[0]
        yield item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM