定制小程序开发费用【爬虫入门】利用flask框架和echarts实现数据可视化

目录



前言

利用flask定制小程序开发费用框架并利用echarts定制小程序开发费用可以对所得到的数据进定制小程序开发费用行可视化分析(定制小程序开发费用变成各种图:饼图、折线图等)


提示:定制小程序开发费用以下是本篇文章正文内容,下面案例可供参考

一、引入

直接pip install flask就可以下载flask。

import flask就可以实现对flask框架的引入。 

二、使用步骤

1.生成本地app

代码如下(示例):

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. return 1
  6. if __name__ == '__main__':
  7. app.run(debug=True)

点击生成的链接就可以进入到本地的app,此时返回1。

然后我们在route中输入不同的字符,就可以接受不同的参数,以此来访问不同的本地地址。比如,我这里不是"/"而是“/login”,那么我们在网页中的后缀中再追加输入login才可以接受返回值1。而在本来的页面中什么也没有。 

app.run()里面的debug默认为False,当设置为True时,我们对代码改动,那么只需要刷新浏览器页面就可以了,而如果是默认值,那么就是需要重启main函数。

2.返回html

需要额外引入库

from flask import Flask,render_template

首先需要在本项目下,也就是和main函数同文件夹下,创造一个名为:templates的文件夹,然后在其中放入或者创建自己需要的html文件。

return render_template('echarts.html')

这就会把html内容的返回出来。

3.使用flask和echarts

首先打开echarts,选择示例的一个好看的例子,符合此次的分析的图。

在准备好的echarts.html的文件里创造一个画图区域,再去bootcdn中找echarts的script,以下内容建议按模板来,这里我选了一个自定义饼图的图。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>第一个 ECharts 实例</title>
  6. <!-- 引入 echarts.js -->
  7. <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
  8. </head>
  9. <body>
  10. <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
  11. <div id="main" style="width: 600px;height:400px;"></div>
  12. <script type="text/javascript">
  13. // 基于准备好的dom,初始化echarts实例
  14. var myChart = echarts.init(document.getElementById('main'));
  15. // 指定图表的配置项和数据
  16. option = {
  17. backgroundColor: '#2c343c',
  18. title: {
  19. text: 'Customized Pie',
  20. left: 'center',
  21. top: 20,
  22. textStyle: {
  23. color: '#ccc'
  24. }
  25. },
  26. tooltip: {
  27. trigger: 'item'
  28. },
  29. visualMap: {
  30. show: false,
  31. min: 80,
  32. max: 600,
  33. inRange: {
  34. colorLightness: [0, 1]
  35. }
  36. },
  37. series: [
  38. {
  39. name: 'Access From',
  40. type: 'pie',
  41. radius: '55%',
  42. center: ['50%', '50%'],
  43. data: [
  44. { value: 335, name: 'Direct' },
  45. { value: 310, name: 'Email' },
  46. { value: 274, name: 'Union Ads' },
  47. { value: 235, name: 'Video Ads' },
  48. { value: 400, name: 'Search Engine' }
  49. ].sort(function (a, b) {
  50. return a.value - b.value;
  51. }),
  52. roseType: 'radius',
  53. label: {
  54. color: 'rgba(255, 255, 255, 0.3)'
  55. },
  56. labelLine: {
  57. lineStyle: {
  58. color: 'rgba(255, 255, 255, 0.3)'
  59. },
  60. smooth: 0.2,
  61. length: 10,
  62. length2: 20
  63. },
  64. itemStyle: {
  65. color: '#c23531',
  66. shadowBlur: 200,
  67. shadowColor: 'rgba(0, 0, 0, 0.5)'
  68. },
  69. animationType: 'scale',
  70. animationEasing: 'elasticOut',
  71. animationDelay: function (idx) {
  72. return Math.random() * 200;
  73. }
  74. }
  75. ]
  76. };
  77. myChart.setOption(option);
  78. </script>
  79. </body>
  80. </html>

 4.传输数据和接受数据

现在我有一个csv的文件,里面有数据内容,现在我要传到html中,怎么办呢?

  1. import pandas as pd
  2. data = pd.read_csv('data.csv')

首先我们先获取csv的内容,同时确定一定要有表头,即:

 确保有name和value,即在设计这个文件时就要加上,因为其中接受的参数名就叫‘name’和‘value’。

如果不是,那么就更改:用rename函数。

data = data.rename(columns={"原来的name":name,"原来的value":value})

然后呢我们需要把这个值传给html让它接收,最后展示。我们知道,这个data接收的是dic类型的数据,那么我们先转换为dic再给,这时候,data为list类型的数据,里面是dic类型。

这时候只需要传入参数并在html中接收就行了。

return render_template('echarts.html',data=data)

 

 双{}接收数据,并把它转为json类型,以适应浏览器来显示。

5.调整参数

 

 min和max根据实际情况来调整。

 亮度默认为0到1,建议为0.5到1。

三、看成果

 这是爬取了某东的书籍的数据展示。最后,给大家奉献出我的全部代码。

main函数:

  1. from selenium.webdriver import Chrome
  2. from selenium.webdriver.common.keys import Keys
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.chrome.options import Options
  5. import time
  6. import csv
  7. from flask import Flask,render_template
  8. import pandas as pd
  9. app = Flask(__name__)
  10. @app.route('/')
  11. def index():
  12. pricedomain1 = 0
  13. pricedomain2 = 0
  14. pricedomain3 = 0
  15. pricedomain4 = 0
  16. pricedomain5 = 0
  17. t = 1
  18. with open('book2.csv', mode='r',encoding='utf-8')as f:
  19. for line in f:
  20. if t == 1:
  21. t += 1
  22. continue
  23. book_a_price = line.split('¥')[-1].strip()
  24. if book_a_price == '' or book_a_price == '"拍拍':
  25. continue
  26. book_a_price = float(book_a_price)
  27. if book_a_price < 30:
  28. pricedomain1 += 1
  29. elif book_a_price < 60:
  30. pricedomain2 += 1
  31. elif book_a_price < 90:
  32. pricedomain3 += 1
  33. elif book_a_price < 120:
  34. pricedomain4 += 1
  35. elif book_a_price > 120:
  36. pricedomain5 += 1
  37. f = open('data.csv', mode='w',encoding='utf-8')
  38. w = csv.writer(f)
  39. w.writerow({'name': 'name', 'value': 'value'})
  40. w.writerow({'0~30':'name', f'{pricedomain1}':'value'})
  41. w.writerow({'30~60': 'name', f'{pricedomain2}': 'value'})
  42. w.writerow({'60~90': 'name', f'{pricedomain3}': 'value'})
  43. w.writerow({'90~120': 'name', f'{pricedomain4}': 'value'})
  44. w.writerow({'120+~': 'name', f'{pricedomain5}': 'value'})
  45. f.close()
  46. data = pd.read_csv('data.csv')
  47. print(data)
  48. data = data.to_dict(orient='records')
  49. return render_template('echarts.html',data=data)
  50. def scrap_data(url, csvname):
  51. f = open(f'{csvname}.csv', mode='w', encoding='utf-8')
  52. w = csv.writer(f)
  53. option = Options()
  54. option.add_argument('--disable-blinkfeatures=AutomationControlled')
  55. web = Chrome(options=option)
  56. page_id = 1
  57. web.get('https://search.jd.com/Search?keyword=python%E7%88%AC%E8%99%AB&pvid=decc839754e0409cbfdcf18b79991ab3&page=')
  58. while True:
  59. time.sleep(2)
  60. name = web.find_elements(By.XPATH, '//*[@id="J_goodsList"]/ul/li/div/div[3]/a/em')
  61. price = (web.find_elements(By.XPATH, '//*[@id="J_goodsList"]/ul/li/div/div[2]'))
  62. for named, priced in zip(name, price):
  63. w.writerow({named.text: 'named', priced.text: 'priced'})
  64. time.sleep(2)
  65. page_id += 1
  66. if page_id > 5:
  67. break
  68. web.find_element(By.XPATH, '//*[@id="J_bottomPage"]/span[1]/a[9]').click()
  69. f.close()
  70. if __name__ == '__main__':
  71. url = 'https://search.jd.com/Search?keyword=python%E7%88%AC%E8%99%AB&pvid=decc839754e0409cbfdcf18b79991ab3&page='
  72. csvname = 'book2'
  73. scrap_data(url, csvname)
  74. app.run()

echarts.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>第一个 ECharts 实例</title>
  6. <!-- 引入 echarts.js -->
  7. <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
  8. </head>
  9. <body>
  10. <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
  11. <div id="main" style="width: 600px;height:400px;"></div>
  12. <script type="text/javascript">
  13. // 基于准备好的dom,初始化echarts实例
  14. var myChart = echarts.init(document.getElementById('main'));
  15. // 指定图表的配置项和数据
  16. option = {
  17. backgroundColor: '#2c343c',
  18. title: {
  19. text: '爬虫书籍价格',
  20. left: 'center',
  21. top: 20,
  22. textStyle: {
  23. color: '#ccc'
  24. }
  25. },
  26. tooltip: {
  27. trigger: 'item'
  28. },
  29. visualMap: {
  30. show: false,
  31. min: 0,
  32. max: 200,
  33. inRange: {
  34. colorLightness: [0.5, 1]
  35. }
  36. },
  37. series: [
  38. {
  39. name: '价格',
  40. type: 'pie',
  41. radius: '55%',
  42. center: ['50%', '50%'],
  43. data: {{data|tojson}}.sort(function (a, b) {
  44. return a.value - b.value;
  45. }),
  46. roseType: 'radius',
  47. label: {
  48. color: 'rgba(255, 255, 255, 0.3)'
  49. },
  50. labelLine: {
  51. lineStyle: {
  52. color: 'rgba(255, 255, 255, 0.3)'
  53. },
  54. smooth: 0.2,
  55. length: 10,
  56. length2: 20
  57. },
  58. itemStyle: {
  59. color: '#c23531',
  60. shadowBlur: 200,
  61. shadowColor: 'rgba(0, 0, 0, 0.5)'
  62. },
  63. animationType: 'scale',
  64. animationEasing: 'elasticOut',
  65. animationDelay: function (idx) {
  66. return Math.random() * 200;
  67. }
  68. }
  69. ]
  70. };
  71. myChart.setOption(option);
  72. </script>
  73. </body>
  74. </html>
网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发