未找到蜘蛛:Django Scrapy集成

2024-09-29 19:36:35 发布

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

我试图在我的Django项目视图中运行一个Scrapy蜘蛛。 这是我的项目结构。你知道吗

ScrapyPro       .
    ├── ScrapyPro
    │   ├── __init__.py
    │   ├── items.py
    │   ├── pipelines.py
    │   ├── settings.py
    │   └── spiders
    │       ├── __init__.py
    │       └── demo_spider.py
    └── scrapy.cfg
    │  
WebCrawlerPro   
    ├── WebCrawlerApp
    │   ├── __init__.py
    │   ├── urls.py
    │   ├── admin.py
    │   ├── apps.py
    │   ├── models.py
    │   ├── views.py
    │
    ├── WebCrawlerPro
    │   ├── __init__.py
    │   ├── urls.py
    │   ├── wsgi.py
    │   ├── settings.py
    └── scrapy.cfg

这里,ScrapyPro是scrapy项目,WebCrawlerPro是Django项目。你知道吗

下面是我试图运行蜘蛛的视图:

from django.shortcuts import render
import urllib
from scrapy.crawler import CrawlerRunner
from scrapy.utils.project import get_project_settings

# Create your views here.
def home_crawl(request):

    if request.method == "POST":
        keyword = request.POST.get("search")
        keyword = urllib.quote(keyword)
        url =( "https://some_example.com/q/%s" % keyword)
        crawler = CrawlerRunner(get_project_settings())
        crawler.crawl('example', url=url)
        crawler.start()

    return render(request,'index.html',{"foo":"bar"})

但是当我在这个视图中通过表单发布请求时。你知道吗

出现以下错误:

KeyError at /
'Spider not found: example'

当我从ScrapyPro项目手动运行spider时:

scrapy crawl example

它执行成功。我做错什么了?你知道吗


Tags: 项目frompyimportproject视图settingsinit

热门问题