与DjangoItem的Scrapy集成会产生错误

时间:2019-07-17 17:38:59

标签: django scrapy

我正在尝试使用DjangoItem抓狂。当我对蜘蛛进行爬网时,出现“ ExampleDotComItem不支持字段:标题”错误。我创建了多个项目,并试图使其正常运行,但始终会遇到相同的错误。我找到了this教程并下载了源代码,并运行了它;我收到相同的错误:

  

回溯(最近通话最近):
  _runCallbacks中的第654行的文件“ c:\ programdata \ anaconda3 \ lib \ site-packages \ twisted \ internet \ defer.py”      current.result =回调(current.result,* args,** kw)   第12行中的文件“ C:\ Users \ A \ Desktop \ django1.7-scrapy1.0.3-master \ example_bot \ example_bot \ spiders \ example.py”        返回ExampleDotComItem(title = title,description = description)    init 中的文件“ c:\ programdata \ anaconda3 \ lib \ site-packages \ scrapy_djangoitem__init __。py”,第29行       super(DjangoItem,self)。初始化(* args,** kwargs)
   init 中的第56行的文件“ c:\ programdata \ anaconda3 \ lib \ site-packages \ scrapy \ item.py”       自我[k] = v
  文件“ c:\ programdata \ anaconda3 \ lib \ site-packages \ scrapy \ item.py”,第66行,   在设置项目中       (self。名称,键))KeyError:'ExampleDotComItem不支持字段:title'

项目结构:

├───django1.7-scrapy1.0.3-master
   ├───example_bot
   │   └───example_bot
   │       ├───spiders
   │       │   └───__pycache__
   │       └───__pycache__
   └───example_project
       ├───app
       │   ├───migrations
       │   │   └───__pycache__
       │   └───__pycache__
       └───example_project
           └───__pycache__

我的Django模型:

from django.db import models

class ExampleDotCom(models.Model):
    title = models.CharField(max_length=255)
    description = models.CharField(max_length=255)

    def __str__(self):
        return self.title

我的“示例”蜘蛛:

from scrapy.spiders import BaseSpider
from example_bot.items import ExampleDotComItem

class ExampleSpider(BaseSpider):
    name = "example"
    allowed_domains = ["example.com"]
    start_urls = ['http://www.example.com/']

    def parse(self, response):
         title = response.xpath('//title/text()').extract()[0]
         description = response.xpath('//body/div/p/text()').extract()[0]
         return ExampleDotComItem(title=title, description=description)

Items.py:

from scrapy_djangoitem import DjangoItem
from app.models import ExampleDotCom

class ExampleDotComItem(DjangoItem):
    django_model = ExampleDotCom

pipelines.py:

class ExPipeline(object):
    def process_item(self, item, spider):
        print(item)
        item.save()
        return item

settings.py:

import os
import sys

DJANGO_PROJECT_PATH = '/Users/A/DESKTOP/django1.7-scrapy1.0.3-master/example_project'
DJANGO_SETTINGS_MODULE = 'example_project.settings' #Assuming your django application's name is example_project

sys.path.insert(0, DJANGO_PROJECT_PATH)
os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
BOT_NAME = 'example_bot'



import django
django.setup()
SPIDER_MODULES = ['example_bot.spiders']

ITEM_PIPELINES = {
    'example_bot.pipelines.ExPipeline': 1000,
}

1 个答案:

答案 0 :(得分:1)

可以显示您的Django模型吗?这可能是由于title模型上未定义ExampleDotCom造成的。

如果在那里,也许您需要运行Django迁移?