scrapy csv输出所有结果在单行 [英] scrapy csv output having all results in single row
问题描述
这里是我的蜘蛛
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
from sample1.items import ppppkartItem
class ppppkartSpider(BaseSpider):
name = "ppppkart"
allowed_domains = ["ppppkart.com"]
start_urls = ["http://www.ppppkart.com/mobilesotracker=nmenu_sub_electronics_0_Mobiles"]
def parse(self, xmlresponse):
sel = Selector(xmlresponse)
sites = sel.xpath('//html/body/div/div[2]/div/div[2]/div[2]/div/div[2]')
items = []
for site in sites:
item = ppppkartItem()
item['image'] = site.xpath('.//a/img/@src').extract()
item['price'] = site.xpath('.//span/text()').extract()
item['title'] = site.xpath('.//a/text()').extract()
item['link'] = site.xpath('.//a/@href').extract()
items.append(item)
return items
这里是我的项目
`
from scrapy.item import Item, Field
class ppppkartItem(Item):
price = Field()
title = Field()
link = Field()
image = Field()
$ b b
这里是我的结果
here is my result
[{"image": ["http://img8a.ppppcart.com/image/mobile/q/f/r/apple-iphone-5c-imadpnhyw2qnxkh5.jpeg", "http://img7a.ppppcart.com/image/mobile/j/z/n/htc-one-max-imadqrqeyceghdba.jpeg",
整个结果是单行,我可以提前预定
the whole result is in single row , i need column wise result what can i do..thanks in advance
我想要结果像
image:img result; price:price result; title:title result; link:link result;
image:2nd img result; price:2nd price result; title:2nd title result; link:2nd link result;
推荐答案
p>
There are few ways of doing this task:
-
您可以使用Scrapy built if功能将项目输出到csv:
You can use Scrapy built if functionality to output item into csv:
scrapy crawl [your spider name] -o items.csv -t csv
scrapy crawl [your spider name] -o items.csv -t csv
有关更多信息,请阅读: http://doc.scrapy.org/en/latest/topics/feed-exports.html#topics- feed-exports
For more information read here: http://doc.scrapy.org/en/latest/topics/feed-exports.html#topics-feed-exports
您可以创建自己的管道并使用项目导出器。那么您将能够对您的项目发生的事情有更多的控制,即您可以删除一些项目等。有关这两个主题的更多信息,请阅读: http://doc.scrapy.org/en/latest/topics/item-pipeline.html 和 http://doc.scrapy.org/en/latest/topics/exporters.html
You can create your own pipeline and use item exporter. then you will be able to have more control over what happens with your item, i.e. you can drop some items etc. For more information about this two topics read: http://doc.scrapy.org/en/latest/topics/item-pipeline.html and http://doc.scrapy.org/en/latest/topics/exporters.html
这篇关于scrapy csv输出所有结果在单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!