scrapy新手:教程dmozItem不支持字段:标题

时间:2016-02-01 09:11:53

标签: python scrapy

var myDropzone = new Dropzone("#dropzone", {
  accept(file) {
    if (file.status == 'added') {

      var newFile = new FS.File(file);

      Images.insert(newFile, function (error, fileObj) {
        if (error) {
          ...
        }
          else {
            ...
          }

            // clear out dropzone
            myDropzone.removeFile(file);
          }
        });
      }
    }
});

最近我正在学习Scrapy。当我测试教程时。我只是从文档中复制代码,但它不能正常工作,显示错误:

import scrapy
from tutorial.items import DmozItem

class DmozSpider(scrapy.Spider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
    ]

    def parse(self, response):
        for sel in response.xpath('//ul/li'):
            item = DmozItem()
            item['title'] = sel.xpath('a/text()').extract()
            item['link'] = sel.xpath('a/@href').extract()
            item['desc'] = sel.xpath('text()').extract()
            yield item

我的items.py写如下:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\scrapy\utils\defer.py", line 102, in iter_errback
yield next(it)
File "C:\Python27\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 28, in process_spider_output
for x in result:
File "C:\Python27\lib\site-packages\scrapy\spidermiddlewares\referer.py", line 22, in <genexpr>
return (_set_referer(r) for r in result or ())
File "C:\Python27\lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 37, in <genexpr>
return (r for r in result or () if _filter(r))
File "C:\Python27\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 54, in <genexpr>
return (r for r in result or () if _filter(r))
File "C:\Users\DannyVim\Desktop\scrapy learning\tutorial\tutorial\spiders\dmoz_spider.py", line 17, in parse
item['title'] = sel.xpath('a/text()').extract()
File "C:\Python27\lib\site-packages\scrapy\item.py", line 63, in __setitem__
(self.__class__.__name__, key))
KeyError: 'DmozItem does not support field: title'

我不知道它有什么问题。我发现这方面的信息很少。那么,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

您忘记了()

中的scrapy.Field()