python爬虫电影数据可视化分析 flask框架_python数据可视化之flask+echarts(一)

之前写了一些小爬虫,数据存储到mongodb之类的数据库,后面觉得单纯的存到数据库用处并不大,如果能够直接展示在页面上就非常的直观了。

这里就简单的说明一下python取数据库中的数据,通过前端js将数据返回。后续再将爬虫结合起来,做一些有意义的数据分析

使用python的web框架Flask作为后台,数据存储使用sqllite3,前端的展示用开源图标插件echarts。

使用sqllite作为数据库存储数据,创建create_db.py,这里是虚拟数据,思路也是参考别人的博客

# coding=utf-8

importsqlite3

importsys

reload(sys)

sys.setdefaultencoding('utf-8')

# 连接

conn = sqlite3.connect('mydb.db')

conn.text_factory =str

c =conn.cursor()

# 创建表

c.execute('''DROP TABLE IF EXISTS weather''')

c.execute('''CREATE TABLE weather (month text, evaporation text, precipitation text)''')

# 数据

# 格式:月份,蒸发量,降水量

purchases = [('1月', 2, 2.6),

('2月', 4.9, 5.9),

('3月', 7, 9),

('4月', 23.2, 26.4),

('5月', 25.6, 28.7),

('6月', 76.7, 70.7),

('7月', 135.6, 175.6),

('8月', 162.2, 182.2),

('9月', 32.6, 48.7),

('10月', 20, 18.8),

('11月', 6.4, 6),

('12月', 3.3, 2.3)

]

# 插入数据

c.executemany('INSERT INTO weather VALUES (?,?,?)', purchases)

# 提交!!!

conn.commit()

# 查询方式一

for row in c.execute('SELECT * FROM weather'):

print(row)

# 查询方式二

c.execute('SELECT * FROM weather')

print(c.fetchall())

# 查询方式二_2

res = c.execute('SELECT * FROM weather')

print(res.fetchall())

# 关闭

conn.close()

使用python的web框架flask,搭建一个简答的后台系统

app.py

importsqlite3from flask importFlask, request, render_template, jsonifyimportsys

reload(sys)

sys.setdefaultencoding('utf-8')

app= Flask(__name__)defget_db():

db= sqlite3.connect('mydb.db')

db.row_factory=sqlite3.Rowreturndbdef query_db(query, args=(), one=False):

db=get_db()

cur=db.execute(query, args)

db.commit()

rv=cur.fetchall()

db.close()return (rv[0] if rv else None) if one elserv

@app.route("/", methods=["GET"])defindex():return render_template("index.html")

@app.route("/weather", methods=["GET"])defweather():if request.method == "GET":

res= query_db("SELECT * FROM weather")return jsonify(month=[x[0] for x inres],

evaporation=[x[1] for x inres],

precipitation=[x[2] for x inres])

@app.route('/map')defmap():return render_template('map.html')if __name__ == "__main__":

app.run(debug=True)

最后在前端页面展示数据,使用百度开源图表插件echarts

ECharts3 Ajax

myChart.setOption({

title: {

text:'异步数据加载示例'},

tooltip: {},

legend: {

data:['蒸发量','降水量']

},

xAxis: {

data: []

},

yAxis: {},

series: [{

name:'蒸发量',

type:'line',

data: []

},{

name:'降水量',

type:'bar',

data: []

}]

});

myChart.showLoading();//显示加载动画//异步加载数据

$.get('/weather').done(function (data) {

myChart.hideLoading();//隐藏加载动画//填入数据

myChart.setOption({

xAxis: {

data: data.month

},

series: [{

name:'蒸发量', //根据名字对应到相应的系列

data: data.evaporation.map(parseFloat)//转化为数字(注意map)

},{

name:'降水量',

data: data.precipitation.map(parseFloat)

}]

});

});

项目结构如下图所示:

最后运行flask项目,python app.py runserver即可,访问127.0.0.1:5000

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值