在 Scrapy 中访问 django 模型 [英] Access django models inside of Scrapy

查看:21
本文介绍了在 Scrapy 中访问 django 模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Scrapy 管道内访问我的 Django 模型,以便我可以将抓取的数据直接保存到我的模型中?

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 get how to set it up?

推荐答案

如果其他人遇到同样的问题,我就是这样解决的.

If anyone else is having the same problem, this is how I solved it.

我将此添加到我的scrapy settings.py 文件中:

I added this to my scrapy settings.py file:

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, filename, desc)       

    setup_environ(project)

setup_django_env('/path/to/django/project/')

注意:上面的路径是你的 django 项目文件夹,而不是 settings.py 文件.

Note: the path above is to your django project folder, not the settings.py file.

现在你可以在你的scrapy项目中完全访问你的django模型.

Now you will have full access to your django models inside of your scrapy project.

这篇关于在 Scrapy 中访问 django 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆