Django自定义命令:AttributeError:“设置”对象没有属性“setmodule”

2024-10-05 12:18:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用Scrapy开发一个webscraper,数据存储在Django中。当我使用自定义命令启动刮板时。但我被这个错误困住了:

AttributeError: 'Settings' object has no attribute 'setmodule'

我检查了设置文件,找不到Setmodule属性

我的自定义命令文件:

/webscraper/management/commands/crawl-test.py

import os
# Django requirements
from django.core.management.base import BaseCommand
from django.conf import settings

# scrapy requirements
import scrapy
from webscraper.webscraper.spiders.WebSpider import WebSpider
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings

class Command(BaseCommand):
    help = "Web Scraper"

    def handle(self, *args, **options):
        process = CrawlerProcess(get_project_settings())
        os.environ['SCRAPY_SETTINGS_MODULE'] = 'webscraper.settings'
        settings_module_path = os.environ['SCRAPY_SETTINGS_MODULE']
        settings.setmodule(settings_module_path, priority='project')
        process = CrawlerProcess(get_project_settings())
        process.crawl(WebSpider)
        process.start()

如果您需要更多信息,请留言


Tags: 文件djangofromimport命令projectgetsettings

热门问题