最近在學習python的過程中發現了一些比較好玩的東西----------爬取微信好友的信息,并可以制作一些酷炫的效果,比如:統計微信好友男女比例、實現圖靈機器人自動回復消息、抓取好友頭像并拼接成圖、獲取好友簽名信息并制作成云圖等。
安裝itchat##
itchat是一個開源的微信個人接口,首先我們先安裝itchat
方法一:可以使用本命令安裝itchat
pip install itchat
方法二(適用于PyCharm):
通過此路徑:File--->Settings----> Project Interpreter----> + 搜索itchat安裝即可
圖靈機器人自動回復消息
如果下面的 apikey無法使用的話,可在圖靈機器人官網(http://www.tuling123.com/)自己注冊一個
import
requests
import
itchat
apikey
=
'1841f627d8924f5ba0bb9ca12e76ad71'
# 定義機器人得到消息并獲取回復內容的函數
def
get_response
(
msg
)
:
apiUrl
=
'http://www.tuling123.com/openapi/api'
data
=
{
'key'
:
apikey
,
'info'
:
msg
,
'userid'
:
'wechat-robot'
}
try
:
r
=
requests
.
post
(
apiUrl
,
data
)
.
json
(
)
# 字典里的get方法在沒有“text”值的時候會返回None,而不會拋出異常
return
r
.
get
(
'text'
)
except
:
# 如果服務器沒能正常交互(返回非json或無法來連接),那么return None
return
# 定義機器人回復的函數
@itchat
.
msg_register
(
itchat
.
content
.
TEXT
)
def
tuling_reply
(
msg
)
:
# 為了保證apikey出現問題時仍舊可以恢復,這里設置一個默認的回復
defaultReply
=
'I received : '
+
msg
[
'Text'
]
# 如果apikey出現問題,那么reply將會是None
reply
=
get_response
(
msg
[
'Text'
]
)
# a or b 的意思是:如果a有內容,那么返回 a,否則返回 b
return
reply
or
defaultReply
# 為了讓實驗過程更加方便(修改程序不用多次掃碼),我們使用熱啟動
itchat
.
auto_login
(
hotReload
=
True
)
itchat
.
run
(
)
統計微信好友男女比例##
import
itchat
import
matplotlib
.
pyplot
as
plt
# 登錄方法,會彈出二維碼,用微信掃描登錄
itchat
.
auto_login
(
)
friends
=
itchat
.
get_friends
(
update
=
True
)
#統計好友的性別,微信中男性為1,女性為2,未知為0
def
sex
(
friends
)
:
total_friends
=
len
(
friends
)
male
=
female
=
other
=
0
for
friend
in
friends
:
sex
=
friend
[
'Sex'
]
if
sex
==
1
:
male
=
male
+
1
elif
sex
==
2
:
female
=
female
+
1
else
:
other
=
other
+
1
sex_list
=
[
"男生"
,
"女生"
,
"不明性別"
]
sex_number_list
=
[
male
,
female
,
other
]
# 此行代碼用來設置中文
plt
.
rcParams
[
'font.sans-serif'
]
=
[
'SimHei'
]
plt
.
title
(
friends
[
0
]
[
'NickName'
]
+
'微信中的男女比例'
)
plt
.
bar
(
range
(
len
(
sex_list
)
)
,
sex_number_list
,
tick_label
=
sex_list
,
color
=
"yellow"
)
plt
.
show
(
)
print
(
"男性好友 : "
,
male
)
print
(
"女性好友 : "
,
female
)
print
(
"不明性別好友 : "
,
other
)
print
(
"好友總數 ; "
,
total_friends
)
# 調用函數sex()
sex
(
friends
)
抓取好友頭像并拼成圖
import
itchat
import
os
from
os
import
listdir
import
math
import
PIL
.
Image
itchat
.
auto_login
(
)
friends
=
itchat
.
get_friends
(
update
=
True
)
user
=
friends
[
0
]
[
"NickName"
]
print
(
user
)
# 建立文件夾來裝好友的頭像----- mkdir()方法用來創建文件夾(目錄)
os
.
mkdir
(
user
)
# 將頭像存到一個文件夾下
number
=
0
for
friend
in
friends
:
img
=
itchat
.
get_head_img
(
userName
=
friend
[
"UserName"
]
)
fileImage
=
open
(
user
+
"/"
+
str
(
number
)
+
".jpg"
,
'wb'
)
fileImage
.
write
(
img
)
fileImage
.
close
(
)
number
+=
1
# listdir()方法用于返回指定文件夾下包含的文件或文件夾的名字的列表,在此處就是:每一張圖片的名字
pics
=
listdir
(
user
)
# 獲取照片的張數
numberPics
=
len
(
pics
)
# 設置每張照片的大小
eachsize
=
int
(
math
.
sqrt
(
float
(
640
*
640
)
/
numberPics
)
)
# 獲取每一行放置多少張圖片
numbline
=
int
(
640
/
eachsize
)
# 設置圖片的大小為 640*640
toImage
=
PIL
.
Image
.
new
(
"RGBA"
,
(
640
,
640
)
)
x
=
0
y
=
0
# pic 為每一張圖片
for
pic
in
pics
:
try
:
# 打開圖片
img
=
PIL
.
Image
.
open
(
user
+
"/"
+
pic
)
except
IOError
:
print
(
"Error : 沒有找到文件或讀取文件失敗"
)
else
:
# 縮小圖片 && ANTIALIAS(抗鋸齒)
img
=
img
.
resize
(
(
eachsize
,
eachsize
)
,
PIL
.
Image
.
ANTIALIAS
)
#拼接圖片 paste()方法,粘貼圖片,在拼接圖片時將圖片一張張粘貼上去
toImage
.
paste
(
img
,
(
x
*
eachsize
,
y
*
eachsize
)
)
x
+=
1
if
x
==
numbline
:
x
=
0
y
+=
1
# 保存拼接后的抬頭像
toImage
.
save
(
user
+
".BMP"
)
# 將拼接好的圖片發送給文件傳輸助手
itchat
.
send_image
(
user
+
".BMP"
,
'filehelper'
)
獲取好友簽名信息并制作成云圖
import
itchat
import
jieba
import
matplotlib
.
pyplot
as
plt
from
wordcloud
import
WordCloud
,
ImageColorGenerator
import
numpy
as
np
import
PIL
.
Image
as
Image
import
re
# 設置登錄方法
itchat
.
auto_login
(
hotReload
=
True
)
friends
=
itchat
.
get_friends
(
update
=
True
)
# 獲取好友簽名信息并儲存在 siglist 中
siglist
=
[
]
for
indedx
,
friend
in
enumerate
(
friends
)
:
sigture
=
friend
[
'Signature'
]
# 如果存在簽名的話
if
len
(
friend
[
'Signature'
]
)
>
0
:
# 將個性簽名中的表情符號去掉(這里沒有去除干凈,利用正則表達式)
sigture
=
sigture
.
replace
(
'span'
,
''
)
.
replace
(
'class'
,
''
)
.
replace
(
'emoji'
,
''
)
.
replace
(
'< ='
,
''
)
.
replace
(
'"'
,
''
)
.
replace
(
''
,
''
)
.
replace
(
'>'
,
''
)
siglist
.
append
(
sigture
)
# 將siglist中的元素拼接為一個字符串
text
=
''
.
join
(
siglist
)
# jieba(結巴分詞:有全模式、精確模式、默認模式、新詞識別、搜索引擎模式)
# jieba.cut()所接收的兩個參數,第一個參數為需要分詞的字符串,第二個為是否采用全模式
word_list
=
jieba
.
cut
(
text
,
cut_all
=
True
)
# 空格拼接
word_space_split
=
' '
.
join
(
word_list
)
# 字體的顏色為對應路徑的背景圖片的顏色
coloring
=
np
.
array
(
Image
.
open
(
"D:/PythonWorkplace/WeChat/image.png"
)
)
# font_path: 字體路徑; random_state: 為每個字體返回一個PIL顏色; scale:按照比例放大畫布;max_font_size:顯示的最大字體的大小
# 如果參數 mask 為空,則使用二維遮罩繪制詞云。如果 mask 非空,設置的寬高值將被忽略,遮罩形狀被 mask 取代
my_wordcloud
=
WordCloud
(
background_color
=
"white"
,
max_words
=
2000
,
mask
=
coloring
,
max_font_size
=
150
,
random_state
=
42
,
scale
=
3
,
font_path
=
"C:/Windows/Fonts/simkai.ttf"
)
.
generate
(
word_space_split
)
# 畫布的顏色
image_colors
=
ImageColorGenerator
(
coloring
)
plt
.
imshow
(
my_wordcloud
.
recolor
(
color_func
=
image_colors
)
)
plt
.
imshow
(
my_wordcloud
)
plt
.
axis
(
"off"
)
plt
.
show
(
)
可承接各種項目,有意者加QQ:1217898975
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
