构建Web API

前面的文章讲述了如何爬取到各种数据(例如:top10系列),如果这些数据能以标准API的方式共享出来,则大大提升数据使用的效率. 于是有了利用AWS EC2主机构建Web API的想法.

步骤

1. EC2 启动http service

EC2默认没有安装http service. 首先利用'sudo yum install httpd'安装httpd, 再用'sudo service httpd restart' 启动http service. 如果你用的不是AMI image,安装方法会有差异,请自行Google确保http service启动成功.

httpd

2. httpd 安装mod_wsgi

数据爬取用的是Python,服务器后端想到的自然是用Python写的flask framework (flask官网)

连接httpd与flask的关键组件是mod_wsgi,同样AMI image默认没有安装,运行'sudo yum install mod_wsgi'安装.

接下来是配置httpd.conf,编写app.wsgi文件,让三者连接起来正常运行.

构建Web API_第1张图片
httpd.conf
构建Web API_第2张图片
app.wsgi

3. 创建flask application

构建Web API_第3张图片
构建Web API_第4张图片

4. JSON data

JSON已逐渐成为事实上的标准,flask framework也提供了API将各种Python数据结构转换成JSON format. 因此这些Web APIs提供JSON data

5. 我的运行结果

构建Web API_第5张图片
API Welcome page


构建Web API_第6张图片
List all available funds


构建Web API_第7张图片
List top10 funds


构建Web API_第8张图片
List top10 funds by fund type

6. Github地址

https://github.com/stonkerlee/FundsRestApiProject

你可能感兴趣的:(构建Web API)