您可以使用Flask框架的Response对象来返回result_callback的结果。以下是一个示例代码:

from flask import Flask, request, Response
from twisted.internet import reactor
from scrapy.crawler import CrawlerRunner
from your_spider_module import JobSpider

app = Flask(__name__)

@app.route('/crawl', methods=['POST'])
def crawl():
    data = request.json

    url = data.get('url')
    if url == None:
        return 'url不能为空'

    configSettings(data)

    # 爬取深度 int
    depth_limit = data.get('depth_limit')

    # 爬取资源类型 list [1:html 2:html图片 3:音频 4:视频] 没配置默认1 2
    types = data.get('types')

    runner = CrawlerRunner(settings=settings)
    d = runner.crawl(JobSpider,start_urls=[url], depth_limit=depth_limit, types=types, result_callback=result_callback)
    d.addBoth(lambda _: reactor.stop())
    reactor.run()

    result = result_callback的结果
    return Response(result, content_type='application/json')

if __name__ == '__main__':
    app.run()

在上面的代码中,Response对象用于返回result_callback的结果,并通过content_type='application/json'指定了返回内容的类型为JSON。您可以根据实际情况调整返回结果的格式和内容类型

approutecrawl methods=POSTdef crawl data = requestjson url = datageturl if url == None return url不能为空 configSettingsdata # 爬取深度 int depth_limit = datagetdepth_lim

原文地址: http://www.cveoy.top/t/topic/iQxe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录