日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

杰洛特的Python之旅01_抓取微信性别数据在web上展现饼图

發(fā)布時間:2024/3/13 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 杰洛特的Python之旅01_抓取微信性别数据在web上展现饼图 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

思路:

1、通過wxpy獲取微信的friends數(shù)據(jù)

2、通過pymongo存儲數(shù)據(jù):人員性別數(shù)據(jù)

3、在pymongo中處理數(shù)據(jù):按性別匯總數(shù)據(jù)

4、利用matplotlib來繪圖

5、計劃利用djongo來做web來展現(xiàn)數(shù)據(jù):簡單的點展現(xiàn)性別餅圖

?第一步:wx.py生成圖片保存到本地

from wxpy import *

import pymongo

from pylab import *

mpl.rcParams['font.sans-serif']=['SimHei']

# 上面兩行代碼解決matplotlib繪圖不能顯示中文問題

import matplotlib.pyplot as plt

bot=Bot(cache_path ="D:\workplace\Python\Scripts\wxpy.pkl")#連接微信

?

my_friends=bot.friends() #sex province

# print(my_friends[0].name)

# print(my_friends[0].sex)

print(len(my_friends))

mylist=[]

m=0

for m in range(len(my_friends)):

?

v_data=[{"name":my_friends[m].name,"sex":my_friends[m].sex}]

mylist=mylist+v_data

m=m+1

#######################################################################################################

#以上獲取微信好友數(shù)據(jù),并組合成一個集合

#######################################################################################################

?

myclient=pymongo.MongoClient("mongodb://localhost:27017/")

mydb=myclient["flydb"]

mycol=mydb["flysite"]

?

# x=mycol.insert_many(mylist) #會出現(xiàn)反復運行插入操作

print(mycol)

for x in mycol.find():

print(x)

?

print(mycol.aggregate([{"$group":{"_id":"$sex","personCount":{"$sum":1}}}]))

cursor=mycol.aggregate([{"$group":{"_id":"$sex","personCount":{"$sum":1}}}])

for c in cursor:

v_list=c.values()

v_list=list(v_list)

?

print(v_list[0])

if list(v_list)[0]==1:

v_men=list(v_list)[1]

elif list(v_list)[0]==2 :

v_women = list(v_list)[1]

elif list(v_list)[0] == 0:

v_other = list(v_list)[1]

print(v_men,v_women,v_other)

#######################################################################################################

##獲取匯總數(shù)據(jù),類別和值的傳入方式需要能適配,目前是固定死的傳入方式

#######################################################################################################

labels=['男性','女性','其他']

sizes=[v_men,v_women,v_other]

explode=(0,0.1,0)

fig1,ax1=plt.subplots()

ax1.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=True,startangle=90)

ax1.axis('equal')

plt.legend()

plt.savefig("D:\\Program Files\\PyCharm 2019.1.1\\untitled1\\static\\sex.png")

###########必須用雙杠\\,不然報錯,Python中\(zhòng)代表轉義

plt.show()

plt.close()

#######################################################################################################

##制作餅圖

#######################################################################################################

?

第二步:django中調用本地圖片

views.py

from django.shortcuts import render_to_response def index(request):return render_to_response('index.html')

?

index.html

<h1>微信性別比例</h1> <img src="/static/sex.png">

urls.py

from laomomo import views #導入views模塊 from django.conf.urls import urlurlpatterns=[url(r'^',views.index),#配置當訪問index/時去調用views下的index方法 ]

settings.py最后一行增加下述兩行,用來默認圖片路徑,解決圖片不現(xiàn)實的問題

STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), ]

總結

以上是生活随笔為你收集整理的杰洛特的Python之旅01_抓取微信性别数据在web上展现饼图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。