![](http://imgs.bizha.top//86af4f43b3905ceb2f27e2ebd2da01dd)
前面两篇用`MongoDB`数据库进行了代理池维护,这篇用这个数据库进行可视化。数据库`Documents`格式为:
```json
{
"_id": {
"$oid": "5ea99e09c1e6fefaa9e4531a"
},
"https": "Socks4://119.146.131.247:8080",
"Location": "中国广东梅州",
"AddTime": "2020年04月29日 23时32分25秒",
"LastUpdate": "2020年04月30日 15时43分16秒"
}
```
对`Location`进行数据分析
![](http://imgs.bizha.top//4718cf2d87d3dabe867d61abb1c3332d)
```
xaxis = list(set(LocationList ))
yaxis = []
for x in xaxis:
y = xaxis.count(x)
yaxis.append(y)
```
### 一、生成柱状图
LocationList = [Proxies['Location'] for Proxies in ProxiesList]
柱状图X轴为`Location`,Y轴为数量.
![](http://imgs.bizha.top//9adcff630d96e5a40de4a849cc0be04b)
柱状图太密集,提取`Location`国家再次生成柱状图。
![](http://imgs.bizha.top//753a1fc20b885822445b2b1020184b47)
再次生成(完整代码)
```
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 16:06:15 2020
@author: Fuwenyue
"""
from pyecharts.charts import Bar
import pymongo
myclient = pymongo.MongoClient('mongodb://fuwenyue:pass4Top@ds049446.mlab.com:49446/proxy',retryWrites='false')
mydb = myclient['proxy']
ProxiesCol = mydb['socks']
ProxiesList = ProxiesCol.find({},{ "_id": 0, "Location": 1}).sort('update',-1)
ProxiesList = [i for i in ProxiesList]
LocationList = [Proxies['Location'] for Proxies in ProxiesList]
LocationList = [Location[0:2] for Location in LocationList]
dic = {}
xaxis = list(set(LocationList))
for x in xaxis:
y = LocationList.count(x)
dic[x] = y
yaxis = [dic[x] for x in xaxis]
bar = (
Bar()
.add_xaxis(xaxis)
.add_yaxis("Socks", yaxis)
)
#bar.render_notebook()
```
![](http://imgs.bizha.top//17cb893d0c0ed1a325acb9eb2e171cc4)
**效果并不好,再次优化,按数量排序**
dict 转 tuple,list
```
d = {'s':2,'e':5,'g':6,'j':7,'m':8,'a':10}
t = tuple(d.items())
print(t)
>>(('s', 2), ('e', 5), ('g', 6), ('j', 7), ('m', 8), ('a', 10))
l = list(t)
print(l)
>>[('s', 2), ('e', 5), ('g', 6), ('j', 7), ('m', 8), ('a', 10)]
```
list tuple排序
```
l = [('s',2),('e',5),('g',6),('j',7),('m',8),('a',10)]
sorted_l=sorted(l,key=lambda t:t[0])
print(sorted_l)
>>[('a', 10), ('e', 5), ('g', 6), ('j', 7), ('m', 8), ('s', 2)]
l = [('s',2),('e',5),('g',6),('j',7),('m',8),('a',10)]
sorted_l=sorted(l,key=lambda t:t[1],reverse=False)
print(sorted_l)
>>[('s', 2), ('e', 5), ('g', 6), ('j', 7), ('m', 8), ('a', 10)]
```
排序柱状图
![](http://imgs.bizha.top//1e741b7588edebcf32b11ca8b3615f9b)
```
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 16:06:15 2020
@author: Fuwenyue
"""
from pyecharts.charts import Bar
import pymongo
myclient = pymongo.MongoClient('mongodb://fuwenyue:pass4Top@ds049446.mlab.com:49446/proxy',retryWrites='false')
mydb = myclient['proxy']
ProxiesCol = mydb['socks']
ProxiesList = ProxiesCol.find({},{ "_id": 0, "Location": 1}).sort('update',-1)
ProxiesList = [i for i in ProxiesList]
LocationList = [Proxies['Location'] for Proxies in ProxiesList]
LocationList = [Location[0:2] for Location in LocationList]
dic = {}
xaxis = list(set(LocationList))
for x in xaxis:
y = LocationList.count(x)
dic[x] = y
l = list(tuple(dic.items()))
sorted_l = sorted(l,key=lambda t:t[1],reverse=True)
xaxis = [x[0] for x in sorted_l]
yaxis = [x[1] for x in sorted_l]
bar = (
Bar()
.add_xaxis(xaxis)
.add_yaxis("Socks", yaxis)
)
#bar.render_notebook()
```
- 【数据可视化】微博热搜排行榜爬虫及数据可视化
- 【数据可视化】bilibili直播排行榜爬虫及数据可视化
- 【互联网】隐藏在哔哩哔哩网页中的彩蛋
- 【爬虫】懒人听书免费部分及已付费部分下载
- 【互联网】搭建各种网盘
- 【互联网】对象储存客户端用作直链网盘
- 【互联网】折腾个手嶌葵的音乐网站
- 【互联网】折腾个音乐网站(进阶版)
- 【软件】Mp3tag的使用与配置
- 【数据库】MongoDB与python的配合使用
- 【爬虫】爬取Socks代理,保存至MongoDB,维护代理池
- 【数据可视化】MongoDB代理池进行数据可视化
- 【软件】Sublime Text 3 的配置与使用
- 【互联网】Apache的. htaccess解决301批量重定向
- 【互联网】AmWiki的安装与使用
- 【互联网】在新浪云(Sae)部署Docker
- 【Termux】Jupyter notebook的安装与使用
- 【Termux】运行自动签到autosignmachine