在Scrapy项目中导入Django模型类

时间:2016-09-24 21:58:43

标签: python django scrapy

我有一个django项目和一个scrapy项目

我想将django模型从scrapy项目导入。

这是我的蜘蛛:

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor

import sys
sys.path.append('/home/ubuntu/venv/dict')
from dmmactress.models import EnActress

class JpnNameSpider(scrapy.Spider):

name = jp_name
allowed_domains = ['enjoyjapan.co.kr']
rx = EnActress.objects.values_list('name', flat=True)
rxs = rx.reverse()
start_urls = ['http://enjoyjapan.co.kr/how_to_read_japanese_name.php?keyword=%s' % jp for jp in rxs]

def parse(self, response):
    for sel in response('//*[@id="contents"]/div/div[1]/div/div[1]'):
        item = JapanessItem()
        item['koname'] = sel.xpath('div[4]/div[1]()/text()').extract()
        item['jpname'] = sel.xpath('div[2]/div[1]()/text()').extract()
        yield item

    next_page = response.css('#contents > div > div:nth-child(4) > div > a::attr(href)').extract_first()
    if naxt_page is not None:
        next_page = response.urljoin(next_page)
        yield scrapy.Request(next_page, self.parse)

我跑蜘蛛时出错了

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configure.

- 有人可以帮我看看我做错了吗?

提前致谢!

1 个答案:

答案 0 :(得分:2)

在使用Django应用程序上下文之外的模型之前,您需要initialize Django

import django 
django.setup()