关闭

利用python+flask实现股票查询与结果网页展示

标签: pythonflask
2101人阅读 评论(0) 收藏 举报
分类:

项目简介

作品通过python+flask构建前端页面实现与用户的交互,其实现过程如下:


部分代码展示

1.后台进行股票查询与结果分析的代码:

# -*- coding:utf-8 -*-
import urllib.request


def get_stock(stock_no):
    # 使用腾讯股票查询接口进行查询
    f = urllib.request.urlopen('http://qt.gtimg.cn/q=s_' + str(stock_no))
    res = f.read().decode('gbk')
    f.close()
    return res


def result_parse(result):
    res_dict = {}
    result_spl = result[14:-3].split('~')
    res_dict['stock_name'] = result_spl[1]
    res_dict['stock_no'] = result_spl[2]
    res_dict['current_price'] = result_spl[3]
    res_dict['fluctuation'] = result_spl[4]
    res_dict['fluctuation_by_percent'] = result_spl[5]
    res_dict['volume'] = result_spl[6]
    res_dict['turnover'] = result_spl[7]
    return res_dict


def stock_check(stock_no):
    if len(stock_no) != 6:
        return 0
    no_first = stock_no[0]
    if int(no_first) == 6:
        code = 'sh' + stock_no
        return code
    elif (int(no_first) == 0) | (int(no_first) == 3):
        code = 'sz' + stock_no
        return code
    else:
        return 0

2.flask交互代码:

# coding:utf8
import configparser

import flask
from flask import render_template

from stork_query import stock_check, result_parse, get_stock

app = flask.Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def homepage():
    if flask.request.method == 'GET':
        result = {}
        return render_template("homepage.html", result=result)
    elif flask.request.method == 'POST'and flask.request.form.get('query', None) == "查询":
        stock_no = flask.request.form['storkcode']
        code = stock_check(stock_no)
        if code != 0:
            result = result_parse(get_stock(code))
            return render_template("homepage.html", result=result)
        else:
            return render_template("homepage.html", warning="请输入正确的股票代码")


if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port=4501)

3.html文件内容

(略)

结果展示


附加事项

具体工程代码可以到http://download.csdn.net/detail/dushu990/9831931进行下载



3
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:36484次
    • 积分:529
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:4篇
    • 译文:0篇
    • 评论:19条
    文章分类
    最新评论