我是靠谱客的博主 顺心唇膏,这篇文章主要介绍flask实现爬虫接口调用,现在分享给大家,希望可以做个参考。

公司让搭建电商类网站的爬虫平台,我用flask简单写了一个接口供同事调用爬虫程序,接口写的并不够好在我看来,我在优化中,后期会把优化好的代码在上传.

import pymongo
from flask import Flask,jsonify,request
from flask_cors import CORS
from gevent import monkey
from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler
import taobao_list
import taobao_details
#异步
# executor = ThreadPoolExecutor(max_workers=2)
# 建立连接,mongo的默认端口是27017
client = pymongo.MongoClient(host='192.168.2.55',port=27017)
# 指定数据库
db = client.spider
#指定集合
collection  = db.snxq
#异步处理相关操作
monkey.patch_all()
# Flask相关变量声明
app = Flask(__name__)

app.config.update(
    DEBUG=True
)
#跨域问题
CORS(app,supports_credentials=True)
#淘宝列表页
@app.route('/tblb',methods=["post","get"])
def taobao():
    keys = request.args.to_dict().get("key")#调用者传参 ky
    #请求进来 输入爬取内容
    taobao_list.main(keys)#这里就是你的爬虫程序接入口
    #爬虫程序
    return jsonify({"code":1})

#淘宝天猫详情页
@app.route('/tbxq',methods=["post","get"])
def taotian():
    keys = request.form.get("key")#调用者传参 ky
    #请求进来 输入爬取内容
    taobao_details.get_detail(keys)#这里就是你的爬虫程序接入口
    #爬虫程序
    return jsonify({"code":1})
if __name__ == '__main__':
    app.run(host='192.168.2.55',port=5555)
    http_server = WSGIServer(('192.168.2.55', 5555), app, handler_class=WebSocketHandler)
    http_server.serve_forever()

最后

以上就是顺心唇膏最近收集整理的关于flask实现爬虫接口调用的全部内容,更多相关flask实现爬虫接口调用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(141)

评论列表共有 0 条评论

立即
投稿
返回
顶部