Как я могу отправить результат scrapy в django views, чтобы frontend мог получить результат scrapy с помощью axios?
Я планирую сделать бэкенд, используя django + scrapy. моя цель состоит в следующем.
frontend(react) посылает методы 'get' от axios в конечную точку django views.
это активирует scrapy, чтобы начать ползать (пауки)
отправляем результат скрапирования в django views.
frontend получает json результат (результат скраппинга, не jobid или файл журнала)
from twisted.internet import reactor
import scrapy
from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging
from scrapyApp.items import ScrapyappItem
from scrapy.utils.project import get_project_settings
class MySpider(scrapy.Spider):
name = "quotes"
def start_requests(self):
urls = [
'https://www.google.com',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
item = ScrapyappItem()
item['title'] = response.css('title::text').get()
yield item
def show1(request):
# configure_logging({'LOG_FORMAT': '%(levelname)s: %(message)s'})
configure_logging({'LOG_FORMAT': '%(levelname)s: %(message)s'})
runner = CrawlerRunner()
d = runner.crawl(MySpider)
d.addBoth(lambda _: reactor.stop())
reactor.run() # the script will block here until the crawling is finished
return HttpResponse({"result":d})