data_api.py
from flask import Flask
from flask_restplus import Api
from flask import request
from flask_restplus import Resource, fields
import json
from weibo_params import news_input, news_output
from gevent import pywsgi
app = Flask(__name__)
# swagger文档标题
api = Api(app, version='1.0', title='gEdi',
description='平台')
news_analysis = api.namespace("NewsAnalysis", "/")
# 参数解析
parser = api.parser()
# 配置post方法需要传递的接口输入参数
news_input = api.model('news_data', news_input)
# 配置返回参数
news_result = api.model('news_result', news_output)
@news_analysis.expect(parser)
class NewsAnalysis(Resource):
"""
新闻集成接口
"""
@news_analysis.doc(body=news_input)
@news_analysis.marshal_with(news_result)
def post(self):
"""
输入某一条新闻信息
"""
receive_params = request.get_data(as_text=True)
receive_params = receive_params.replace("\\\\", "\\")
print(receive_params)
post_value = json.loads(receive_params,
encoding="utf-8")
print("post_value", post_value)
weibo_id = post_value['weibo_id']
results = {
"weibo_id": weibo_id
}
print(results)
return results, 200
# 配置各命名空间下的接口
news_analysis.add_resource(NewsAnalysis, '/')
if __name__ == '__main__':
# app.run(host="127.0.0.1", port="5003", debug=True)
server = pywsgi.WSGIServer(('127.0.0.1', 5000), app)
server.serve_forever()
weibo_params.py
from flask_restplus import fields
default_data = {
"weibo_id": "3741282071656011",
"content": "携带犬粮入境 一律没收销毁",
"url": "https://m.weibo.cn/status/BhjVZ6WNR?mblogid=BhjVZ6WNR&luicode=10000011&lfid=100103type%3D1%26q%3D%E8%BF%9B%E5%87%BA%E5%A2%83",
"date": "2014-08-08 13:10:18",
"keyword": "进出境",
"author": "贵州日报官微",
"crawl_time": "2021-09-23",
"is_verified": "1",
"verified_type": "3"
}
news_input = {
"weibo_id": fields.String(required=True, description=''),
"content": fields.String(required=False, description='',
default=default_data["content"]),
"url": fields.String(required=False, description='',
default=default_data["url"]),
"date": fields.String(required=False, description='',
default=default_data["date"]),
"keyword": fields.String(required=False, description='',
default=default_data["keyword"]),
"author": fields.String(required=False, description='',
default=default_data["author"]),
"crawl_time": fields.String(required=False, description='',
default=default_data["crawl_time"]),
"is_verified": fields.String(required=False, description='',
default=default_data["is_verified"]),
"verified_type": fields.String(required=False, description='',
default=default_data["verified_type"])
}
news_output = {
"weibo_id": fields.String(required=True, description='')
}
运行data_api.py文件即可实现
测试方法:
1)将上述程序跑起来以后,在浏览器中输入:http://127.0.0.1:5000/