如何从django运行外部脚本?

时间:2015-10-01 04:25:49

标签: python django

我正在尝试用django做基本的关键字工具,到目前为止我设置了表和CVS导入选项。现在,我想使用外部谷歌刮刀脚本(https://github.com/NikolaiT/GoogleScraper)在将这些关键字添加到django的ORM后对其进行分析(谷歌每个关键字,并为后面的分析保存前10个结果)。

在django之外运行此脚本很简单:

import sys
from GoogleScraper import scrape_with_config, GoogleSearchError
from GoogleScraper.database import ScraperSearch, SERP, Link

# very basic usage
def basic_usage():
    # See in the config.cfg file for possible values
    config = {
        'SCRAPING': {
...
if __name__ == '__main__':

usage = 'Usage: {} [basic|image]'.format(sys.argv[0])
if len(sys.argv) != 2:
    print(usage)
else:
    arg = sys.argv[1]
    if arg == 'basic':
...

https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

这是最好的方法吗?

是否有一些类似的脚本在Django中实现,我可以看到和学习​​:)?

干杯

1 个答案:

答案 0 :(得分:0)

只需将脚本放在app目录中,然后就可以在视图中导入

from GoogleScraper import basic_usage

def view_fun(request):
    basic_usage()
    return Response
相关问题