首页 > 技术文章 > 20170724_python+mysql+php+echarts获取和显示某数字货币网站数据

tiantian123 2017-07-24 19:06 原文

最近在玩数字货币,但是上班时间没得玩又想看看数据,于是就想到了保存数据,下班再看 - - 

先用python的request每秒获取接口数据,然后保存在mysql中,如下

import requests
import json
import time
import pymysql.cursors

conn = pymysql.Connect(user='root', password='root', database='bi',charset="utf8")
cursor = conn.cursor()

url = 'https://www.xxx.com/market/depth?symbol=ethcny&type=step5'

headers = { "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding":"gzip, deflate, br",
            "Accept-Language":"zh-CN,zh;q=0.8",
            "Cache-Control":":max-age=0",
            "Connection":"keep-alive",
            "Cookie":"_ga=GA1.2.1136622599.1500602038; __jsluid=5605739c5bd985046925ddfe0025cc3d; Hm_lvt_b5afd6d7387e7dde50eb21849ba44094=1500602039; Qs_lvt_93589=1500602039; Qs_pv_93589=713589804225373000",
            "Host":"be.huobi.com",
            "Upgrade-Insecure-Requests":"1",
            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
            }

def getTick():
    res = requests.get(url,headers=headers)

    if res.status_code == 200:
        val = json.loads(res.text)

        utime = int(time.time())
        asks = val['tick']['asks'][0][0]
        bids = val['tick']['bids'][0][0]
        asksnum = val['tick']['asks'][0][1]
        bidsnum = val['tick']['bids'][0][1]

        cursor.execute('insert into tick (utime, asks, bids, asksnum, bidsnum) values (%s, %s, %s, %s, %s)', [utime, asks, bids, asksnum, bidsnum ])
        cursor.rowcount
        conn.commit()

        timeArray = time.localtime(utime)
        strTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
        print('Time:'+strTime)

while True:
    getTick()
    time.sleep(1)

然后使用php+echarts显示数据

<?php
header("Content-Type: text/html; charset=UTF-8");

$con = mysql_connect('127.0.0.1', 'root', 'root');
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

$startTime = strtotime($_POST['starttime']);
$endTime = strtotime($_POST['endtime']);

$sql = sprintf("select * from tick where utime > %s and utime < %s", $startTime, $endTime);

mysql_select_db("bi", $con);

$result = mysql_query($sql);

$data['name'] = array(0=>'asks', 1=>'bids');
$i = 0;
while ($row = mysql_fetch_array($result)) {
    $data['series'][0]['data'][$i] = $row['asks']; 
    $data['series'][1]['data'][$i] = $row['bids']; 
    $data['time'][$i++] = date("H:i:s",$row['utime']); 
}
$data['maxnum'] = $i;
mysql_close($con);
echo json_encode($data);
?>
<html>

<head>
    <title>Bootstrap</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

    <hr>
    开始时间<input type="datetime-local" id="starttime" step=1 />
    结束时间<input type="datetime-local" id="endtime" step=1 />
    <hr>

    <button id="get_data">获取数据</button>
    <button id="mytext" onclick="do_click()">持续测试</button>
    <div id="main" style="height:600px;width: 100%;"></div>

    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <script src="https://cdn.bootcss.com/echarts/3.6.2/echarts.common.js"></script>

    <script type="text/javascript">
        // 基于准备好的dom,初始化echarts图表
        var myChart = echarts.init(document.getElementById("main"));

        function set_data(jsondata) {
            series = [];
            $.each(jsondata['series'], function (key, val) {
                var addjson = { 'name': jsondata['name'][key], 'type': 'line', 'data': jsondata['series'][key]['data'] };
                series.push(addjson);
            });
            var option = {
                title: {
                    text: 'text',
                    subtext: 'subtext'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: jsondata['name']
                },
                //右上角工具条
                toolbox: {
                    show: true,
                    feature: {
                        mark: {
                            show: true
                        },
                        dataView: {
                            show: true,
                            readOnly: false
                        },
                        magicType: {
                            show: true,
                            type: ['line', 'bar']
                        },
                        restore: {
                            show: true
                        },
                        saveAsImage: {
                            show: true
                        }
                    }
                },
                calculable: true,
                xAxis: [{
                    type: 'category',
                    boundaryGap: false,
                    data: jsondata['time']
                }],
                yAxis: [{
                    type: 'value',
                    scale: true,
                    axisLabel: {
                        formatter: '{value} '
                    }
                }],
                series: series
            };

            // 为echarts对象加载数据
            myChart.setOption(option);
        }
    </script>

    <script>
        $("#get_data").click(function () {
            $.ajax({
                type: "post",
                url: "/echeart.php",
                data: { 'starttime': $("#starttime").val(), 'endtime': $("#endtime").val() },
                async: true,
                dataType: 'json',
                success: function (msg) {
                    set_data(msg);
                }
            });
        });
    </script>

    <script>
        var format = "";

        //构造符合datetime-local格式的当前日期
        function getFormat() {
            format = "";
            var nTime = new Date();
            format += nTime.getFullYear() + "-";
            format += (nTime.getMonth() + 1) < 10 ? "0" + (nTime.getMonth() + 1) : (nTime.getMonth() + 1);
            format += "-";
            format += nTime.getDate() < 10 ? "0" + (nTime.getDate()) : (nTime.getDate());
            format += "T";
            format += nTime.getHours() < 10 ? "0" + (nTime.getHours()) : (nTime.getHours());
            format += ":";
            format += nTime.getMinutes() < 10 ? "0" + (nTime.getMinutes()) : (nTime.getMinutes());
            format += ":";
            format += nTime.getSeconds() < 10 ? "0" + (nTime.getSeconds()) : (nTime.getSeconds());
        }

        $(function () {
            getFormat();
            var starttime = format.substring(0, format.length - 2) + '00';
            $("#starttime").attr('value', starttime);//赋初始值
            $("#endtime").attr('value', format);//赋初始值
        })

    </script>
    <script>
            //$('#get_data').click();
            mystate = 0;
            var isdo;
            function do_click() {
                if (mystate == 0) {
                    mystate = 1;
                    isdo = setInterval("$('#get_data').click();", 500);
                    $('#mytext').html('停止');
                } else {
                    clearInterval(isdo);
                    mystate = 0;
                    $('#mytext').html('持续查看');
                }
            }
    </script>
</body>
</html>

前端功能还没做完,等有空了完善一下.

从上午运行到下午,获取了近2w条数据,还是比较稳定的.明天上效果图!

 

推荐阅读