加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何在scrapy pipelines.py文件中导入django模型

发布时间:2020-12-20 13:33:20 所属栏目:Python 来源:网络整理
导读:我正在尝试在我的pipelines.py中导入一个 django应用程序的模型,以使用django orm保存数据.我在第一个涉及django应用程序“app1”中创建了一个scrapy项目scrapy_project(顺便说一句,这是一个不错的选择吗?). 我将这些行添加到我的scrapy设置文件中: def se
我正在尝试在我的pipelines.py中导入一个 django应用程序的模型,以使用django orm保存数据.我在第一个涉及django应用程序“app1”中创建了一个scrapy项目scrapy_project(顺便说一句,这是一个不错的选择吗?).
我将这些行添加到我的scrapy设置文件中:

def setup_django_env(path):
  import imp,os
  from django.core.management import setup_environ

  f,filename,desc = imp.find_module('settings',[path])
  project = imp.load_module('settings',f,desc)

  setup_environ(project)

current_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
setup_django_env(os.path.join(current_dir,'../../d_project1'))

当我尝试导入我的django应用程序app1的模型时,我收到以下错误消息:

Traceback (most recent call last):
  File "/usr/local/bin/scrapy",line 4,in <module>
    execute()
  File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py",line 122,in execute
    _run_print_help(parser,_run_command,cmd,args,opts)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py",line 76,in     _run_print_help
    func(*a,**kw)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py",line 129,in     _run_command
    cmd.run(args,opts)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/commands/crawl.py",line 43,in     run
    spider = self.crawler.spiders.create(spname,**opts.spargs)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/command.py",line 33,in crawler
    self._crawler.configure()
  File "/usr/local/lib/python2.7/dist-packages/scrapy/crawler.py",line 41,in configure
    self.engine = ExecutionEngine(self,self._spider_closed)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/core/engine.py",line 63,in     __init__
    self.scraper = Scraper(crawler)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/core/scraper.py",line 66,in     __init__
    self.itemproc = itemproc_cls.from_crawler(crawler)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/middleware.py",line 50,in     from_crawler
    return cls.from_settings(crawler.settings,crawler)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/middleware.py",line 29,in     from_settings
    mwcls = load_object(clspath)
  File "/usr/local/lib/python2.7/dist-packages/scrapy/utils/misc.py",line 39,in     load_object
    raise ImportError,"Error loading object '%s': %s" % (path,e)
ImportError: Error loading object 'scrapy_project.pipelines.storage.storage': No module         named dydict.models

为什么不能scrapy访问django应用程序模型(在installed_app中给出app1)?

解决方法

尝试:

from .. models import MyModel

要么

from ... models import MyModel

每个点代表位置

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读