[问题]scrapy写好pipelines执行,却出现KeyError

楼主: allen511081 (蓝)   2015-01-04 00:02:55
各位好,小弟之前发问的问题,经过各位大大前辈的指点,
已经解决了,但是小弟今天又遇到一个小问题,写完pipelines后,
一执行,就会出现KeyError,参考
http://www.hackhowtofaq.com/blog/web-scraping-with-scrapy/,
写出我的pipelines,以下是我的程式码
pipelines.py:
import csv
class testPipeline(object):
def__init__(self):
self.myCSV = csv.writer(open('birds.csv', 'wb'))
self.myCSV.writerow(['title', 'birdName','count'])
def process_item(self, item,spider):
self.myCSV.writerow([item['title'].encode('utf-8'),
item['birdName'].encode('utf-8'),
item['count'].encode('utf-8')])
return item
一直出现这两个错误
1. File "D:\python27\allentest\allentest\pipelines.py", line 17, in
process_item
self.myCSV.writerow([item['title'].encode('utf-8'),
exceptions.AttributeError: 'list' object has no attribute 'encode'
2. File "D:\python27\lib\site-packages\scrapy\item.py", line 50, in __getitem__
return self._values[key]
exceptions.KeyError: 'title'
可是我参照范例写出来的,请问我该怎样修改?
作者: jimmytzeng (jimmytseng)   2015-01-04 08:03:00
把encode('utf-8')去掉看看
楼主: allen511081 (蓝)   2015-01-04 08:31:00
回楼上大大,encode删除后,还是会出现KeyError而且title、birdName、count,都会出现keyError
作者: AndCycle (AndCycle)   2015-01-04 13:00:00
KeyError就是没那个key, 你该先查为什么你传的item没有
楼主: allen511081 (蓝)   2015-01-06 10:10:00
给楼上大大,小弟本人还是新手,还望大大指导一下

Links booklink

Contact Us: admin [ a t ] ucptt.com