Python爬微信好友頭像,性別,所在地區
本文適合新手(有一定基礎的小白)
今天沒事,用的網頁版微信,於是看原始碼心理作怪,F12開啟,研究了一下,結果發現
/斜眼笑/斜眼笑/斜眼笑
再加上,沒事幹,(大家有好工作求介紹,本人待就業),Python也很久沒弄了,於是(說幹就幹)
開啟Python
importrequests importjson from collections import Counter from pyecharts import Pie import hashlib
這裡是所要用到的庫檔案
我們想說思路
第一步,獲取Url資料
第二步,看看是不是反爬
第三步,取出資料研究型別
第四步 ,篩選資料,統計圖表
第五步,圖片下載
第一 獲取Url
貌似這裡有
第二步,看看防爬嘛(應該反爬,畢竟這麼大的公司,所以,管他防不防,都加headers
headers = { 'Cookie': ' ~自己的 ', 'Host': 'wx.qq.com', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' }
第三步,得出資料
資料是這樣的
dict1 = json.loads(list_data.text) print("%s: %s" % ("好友數" ,dict1["MemberCount"]))#字典 listdata=dict1["MemberList"]# 集合
forlistsinrange(0,dict1["MemberCount"]):# 把全部使用者的地址儲存 HeadImgUrls.append(listdata[lists]["HeadImgUrl"]) sexs.append(listdata[lists]["Sex"]) Province.append(listdata[lists]["Province"]+""+listdata[lists]["City"]) rest=Counter(Province)#這裡是分組
第四步 ,篩選資料,統計圖表
pie = Pie("微信好友使用者統計:好友%s" %( +dict1["MemberCount"]), title_pos='center',width=1400,height=600) pie.add( "", countProvin, Countcity, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left", ) pie.render()
未知是公眾號
因為資料是 1為男, 2為女 0為公眾號
資料顯示這裡我也不知道怎麼講,可以去看文件
http://pyecharts.org/#/zh-cn/charts_configure
第五步,圖片下載
獲取圖片地址。。。
然後下載
程式碼
# coding:utf8 importrequests importjson from collections import Counter from pyecharts import Pie import hashlib # 微信Url資料獲取連線 Wxurl="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&r=1551517351463&seq=0&skey=@crypt_80cc7620_ac3680d314a5860438086e5d54cf177d" headers = { Cookie': 'cookie', 'Host': 'wx.qq.com', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36' } #TODOProvince_City 使用到的陣列集合 Province = [] Countcity=[] #TODO儲存性別 sexs=[] #TODO儲存圖片 HeadImgUrls=[] def SexFun(Sexrests): # 性別方法 print("男%s" % Sexrests[1]) print("女%s" % Sexrests[2]) print("公眾號%s" % Sexrests[0]) pie = Pie("微信好友性別統計" , title_pos='center', width=1400, height=600) pie.add( "", ['男','女','未知'], [Sexrests[1],Sexrests[2],Sexrests[0]], radius=[40, 75], title='sex', label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left", ) pie.render('sex.html') print(Sexrests) #TODOWechar_data方法 def Wechar_data(Wxurl,headers): globalProvince# 轉換為全域性變數,假如不轉換的話,會報錯的 globalCountcity global sexs global HeadImgUrls list_data = requests.get(Wxurl,headers=headers) list_data.encoding = "utf-8" print(list_data.text) dict1 = json.loads(list_data.text) print("%s: %s" % ("好友數" ,dict1["MemberCount"]))#字典 listdata=dict1["MemberList"]# 集合 #print(listdata) forlistsinrange(0,dict1["MemberCount"]):# 把全部使用者的地址儲存 HeadImgUrls.append(listdata[lists]["HeadImgUrl"]) sexs.append(listdata[lists]["Sex"]) Province.append(listdata[lists]["Province"]+""+listdata[lists]["City"]) rest=Counter(Province)#這裡是分組 #print(rest) """ Counter({'': 29, '湖南郴州': 21, '湖南長沙': 16, '廣東深圳': 14, '廣東中山': 9, '廣東廣州': 8, '北京朝陽': 7, '湖南婁底': 4, '湖南懷化': 4, '北京海淀': 3, '湖南益陽': 3, '上海浦東新區': 3, '安徽合肥': 2, '北京西城': 2, '廣東佛山': 2, '廣東湛江': 2, '湖南株洲': 2, '浙江杭州': 1, '北京東城': 1, '廣東': 1, '四川德陽': 1, '遼寧丹東': 1, '河南三門峽': 1, '湖南張家界': 1, '廣東肇慶': 1, '上海長寧': 1, '澳門路環島': 1, '江蘇': 1, '上海': 1, '湖南衡陽': 1, '河南南陽': 1, '湖南永州': 1, '北京': 1, 'North Shore': 1, '湖北恩施': 1, '湖南湘潭': 1, '湖南嶽陽': 1, '湖南': 1, 'EnglandSheffield': 1, '湖南邵陽': 1, '湖北武漢': 1, '廣東珠海': 1, 'Eastern': 1, '江西南昌': 1, 'SabahSemporna': 1, '四川成都': 1, '北京昌平': 1, '福建寧德': 1}) """ sets = set(Province) countProvin = list(sets) #print(countProvin)#city '''['', '河南三門峽', '河南南陽', '北京', '廣東肇慶', '廣東中山', '上海長寧', '湖南長沙', '廣東佛山', '福建寧德', '廣東深圳', '湖南婁底', '遼寧丹東', '浙江杭州', '湖北恩施', 'North Shore', '湖南', '北京昌平', '上海浦東新區', '澳門路環島', '湖南株洲', '湖南邵陽', 'SabahSemporna', '廣東廣州', '湖南張家界', '湖南衡陽', '湖南永州', '湖北武漢', '北京朝陽', '安徽合肥', '北京西城', '湖南嶽陽', '廣東', '北京東城', '江西南昌', '廣東珠海', '四川德陽', '湖南郴州', '湖南益陽', 'EnglandSheffield', '四川成都', '廣東湛江', '江蘇', '湖南懷化', 'Eastern', '湖南湘潭', '上海', '北京海淀']''' #print(en(countProvin))48 foriiiinrange(0,len(countProvin)): #print(countProvin[iii]) Countcity.append(rest[countProvin[iii]]) #print(Countcity)# numlist try: countProvin[0]="暫未填寫地區" exceptException: print("登入授權已過期") pie = Pie("微信好友使用者統計:好友%s" %( +dict1["MemberCount"]), title_pos='center',width=1400,height=600) pie.add( "", countProvin, Countcity, radius=[40, 75], label_text_color=None, is_label_show=True, legend_orient="vertical", legend_pos="left", ) pie.render() #性別判斷 Sexrests = Counter(sexs)# 這裡是分組 #讓程式碼看上去簡單一點使用方法封裝 SexFun(Sexrests) HeadimgFun(HeadImgUrls,headers) def HeadimgFun(imgdata,headers): #print(imgdata[0]) md = hashlib.md5()# 構造一個md5 for i in range(0,len(imgdata)): utllist="https://wx.qq.com"+imgdata[i] reqs = requests.get(utllist,headers=headers) reqs.encoding = "utf-8" md.update(imgdata[i].encode()) imgname=md.hexdigest()+".jpg" withopen('./images/'+imgname, 'wb') asf: f.write(reqs.content) Wechar_data(Wxurl,headers) demo
不懂可以問我,覺得好就點個贊哈