作者allen511081 (蓝)
看板Python
标题[问题]scrapy写好pipelines执行,却出现KeyError
时间Sun Jan 4 00:02:55 2015
各位好,小弟之前发问的问题,经过各位大大前辈的指点,
已经解决了,但是小弟今天又遇到一个小问题,写完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'
可是我参照范例写出来的,请问我该怎样修改?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.127.51.228
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1420300980.A.7D7.html
1F:推 jimmytzeng: 把encode('utf-8')去掉看看 01/04 08:03
2F:→ allen511081: 回楼上大大,encode删除後,还是会出现KeyError 01/04 08:31
3F:→ allen511081: 而且title、birdName、count,都会出现keyError 01/04 08:33
4F:→ AndCycle: KeyError就是没那个key, 你该先查为什麽你传的item没有 01/04 13:00
5F:→ allen511081: 给楼上大大,小弟本人还是新手,还望大大指导一下 01/06 10:10