我只是想写一个csv。不过,我有两个单独的for语句,因此每个for语句的数据独立导出并打破顺序。建议?Python:Scrapy CSV导出不正确?
def parse(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select('//td[@class="title"]')
subtext = hxs.select('//td[@class="subtext"]')
items = []
for title in titles:
item = HackernewsItem()
item["title"] = title.select("a/text()").extract()
item["url"] = title.select("a/@href").extract()
items.append(item)
for score in subtext:
item = HackernewsItem()
item["score"] = score.select("span/text()").extract()
items.append(item)
return items
从下图中可以看出,第二个for-statement打印在其他打印机下面,而不打印在其他打印机的“其他”之下。
CSV图像附:
和GitHub的链接,完整的文件:https://github.com/nchlswtsn/scrapy/blob/master/items.csv
您可以上传CSV文件吗? – ahmed
@boussouira是!更新 –