{r setup, include=FALSE} knitr::opts_chunk$set(eval = FALSE) itchat包提取微信好友公开数据
Terminal中pip install itchat安装python包。
import itchat
itchat.login()
在点击itchat.login()后,会产生一个二维码,用手机扫描这个二维码,相当于在另外一个客户端登陆了。
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 李家翔
成功登陆的信息。
friends = itchat.get_friends(update=True)[0:]
friends是一个json文件。
看下哪些变量可以研究。
import pandas as pd
pd.DataFrame(friends).columns
Index(['Alias', 'AppAccountFlag', 'AttrStatus', 'ChatRoomId', 'ChatRoomOwner',
'City', 'ContactFlag', 'ContactType', 'DisplayName', 'EncryChatRoomId',
'HeadImgFlag', 'HeadImgUpdateFlag', 'HeadImgUrl', 'HideInputBarFlag',
'IsOwner', 'KeyWord', 'MemberCount', 'MemberList', 'NickName',
'OwnerUin', 'PYInitial', 'PYQuanPin', 'Province', 'RemarkName',
'RemarkPYInitial', 'RemarkPYQuanPin', 'Sex', 'Signature', 'SnsFlag',
'StarFriend', 'Statues', 'Uin', 'UniFriend', 'UserName', 'VerifyFlag',
'WebWxPluginSwitch'],
dtype='object')
Sex性别可以算吧。
pd.DataFrame(friends).Sex.unique()
array([1, 2, 0])
key是Sex,男性值为1,女性为2。
pd.DataFrame(friends).groupby("Sex").agg(['count'])
Sex
0 34
1 244
2 264
key是Sex,男性值为1,女性为2。
0是什么情况。 500个好友,正常。
感觉都是EDA的范畴的,直接导出数据,在R中搞。
pd.DataFrame(friends).to_excel("friends.xlsx")
接下来就是对friends.xlsx分析了。
<!-- ``` -->
<!-- library(readxl) -->
<!-- friends <- read_excel("~/Desktop/friends.xlsx") -->
<!-- library(tidyverse) -->
<!-- ``` -->
<!-- ``` -->
<!-- friends1 <- friends %>% -->
<!-- select(-X__1) -->
<!-- ``` -->
<!-- ``` -->
<!-- friends1 %>% -->
<!-- mutate(Sex = Sex %>% -->
<!-- as.factor() %>% -->
<!-- fct_recode(., -->
<!-- other = "0", -->
<!-- male = "1", -->
<!-- female = "2" )) %>% -->
<!-- ggplot(aes(x = Sex)) + -->
<!-- geom_bar(stat = "count", width = 0.5) -->
<!-- ``` -->
<!-- ``` -->
<!-- friends1 %>% -->
<!-- group_by(Province) %>% -->
<!-- count() %>% -->
<!-- arrange(desc(n)) -->
<!-- ``` -->
- jiebaR包做文本清洗 - R和Python的学习笔记 对签名进行文本分析。
Signature这个变量。
<!-- # ``` -->
<!-- # library(jiebaR) -->
<!-- # engine <- worker() -->
<!-- # engine -->
<!-- # friends2 <- -->
<!-- # friends1 %>% -->
<!-- # select(Signature) %>% -->
<!-- # filter(!is.na(Signature)) %>% -->
<!-- # .$Signature %>% -->
<!-- # paste(collapse = '') -->
<!-- # ``` -->
<!-- ``` -->
<!-- # friends2 -->
<!-- segment(friends2, engine) -->
<!-- ``` -->
可以看到这种"正式迈入双宝妈咪的行列,怀挺!",意思。
<!-- ``` -->
<!-- friends1 %>% -->
<!-- select(Signature) %>% -->
<!-- filter(!is.na(Signature)) %>% -->
<!-- # mutate(Signature = str_replace_all(Signature, -->
<!-- # "<span class=*></span>","" -->
<!-- # )) %>% -->
<!-- mutate(len = str_length(Signature)) %>% -->
<!-- arrange(desc(len)) -->
<!-- ``` -->
itchat包还有很多其他的功能还有待发掘,包括自动回复微信信息、自动添加好友、管理微信群等,有时间再慢慢摸索吧。