Access django models inside of Scrapy

后端 未结 8 2260
执笔经年
执笔经年 2020-11-28 19:11

Is it possible to access my django models inside of a Scrapy pipeline, so that I can save my scraped data straight to my model?

I\'ve seen this, but I don\'t really

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 19:53

    For Django 1.4, the project layout has changed. Instead of /myproject/settings.py, the settings module is in /myproject/myproject/settings.py.

    I also added path's parent directory (/myproject) to sys.path to make it work correctly.

    def setup_django_env(path):
        import imp, os, sys
        from django.core.management import setup_environ
    
        f, filename, desc = imp.find_module('settings', [path])
        project = imp.load_module('settings', f, filename, desc)       
    
        setup_environ(project)
    
        # Add path's parent directory to sys.path
        sys.path.append(os.path.abspath(os.path.join(path, os.path.pardir)))
    
    setup_django_env('/path/to/django/myproject/myproject/')
    

提交回复
热议问题