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()

猜你喜欢

转载自blog.csdn.net/weixin_42603784/article/details/88692375