看板 Python 關於我們 聯絡資訊
各位好,小弟之前發問的問題,經過各位大大前輩的指點, 已經解決了,但是小弟今天又遇到一個小問題,寫完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://www.ptt.cc/bbs/Python/M.1420300980.A.7D7.html
jimmytzeng: 把encode('utf-8')去掉看看 01/04 08:03
allen511081: 回樓上大大,encode刪除後,還是會出現KeyError 01/04 08:31
allen511081: 而且title、birdName、count,都會出現keyError 01/04 08:33
AndCycle: KeyError就是沒那個key, 你該先查為什麼你傳的item沒有 01/04 13:00
allen511081: 給樓上大大,小弟本人還是新手,還望大大指導一下 01/06 10:10