公司让搭建电商类网站的爬虫平台,我用flask简单写了一个接口供同事调用爬虫程序,接口写的并不够好在我看来,我在优化中,后期会把优化好的代码在上传.
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49import 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实现爬虫接口调用内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复