首页 > 编程知识 正文

python微信开发实例,python中find的返回值是

时间:2023-05-05 19:18:16 阅读:166754 作者:4303

1、wxpy最近研究了微信的玩法。 我们可以通过网络版的微信网络版,扫码登录后,抓起包去获取信息。 另外,还可以开机自检并发送信息。

然后,我们发现了一个名为wxpy的库。 已经完成了微信的界面,大大方便了我们的微信挖掘。 以下功能也通过wxpy实现了。

安装名为wxpy的库

pip install wxpy

首先进行简单的试用,实现微信的登录。 执行以下代码将生成二维码。 扫码后,在手机端确认登录。 按照惯例,先把“‘hello world”发给自己。 语句是bot.file _ helper.send (“hello world! ”,实际上是通过文件传输助手发送的消息,也可以使用bot.self.send(「helloworld”)。

fromwxpyimport * bot=bot (cache _ path=true ) bot.file_helper.send ) ' helloworld!' )其中cache_path=True可以避免每次登录都需要重新扫描,具有缓存的作用。

我不仅能给自己发信息,还能给朋友发信息了

bot=bot(cache_path=true ) my_friend=bot.friends ).search (马面(0) my_friend.send ) Helloworrd ) ) #所有好友friends=bot.friends(#遍历输出好友名称forfriendinfriends3360print ) friend ) #所有聊天组groups=bot.groups ) fortind 找到' )不仅登录和发信息,我们还可以这样来玩,下去~

2、微信好友男女比例想统计自己微信好友的性别比例,当然简单,首先要获得好友列表,统计并统计列表性别

fromwxpyimport * defget _ friend _ sex (friends ) : male=female=other=0foriinfriends [ 1: ] 3360 sex=I.sexifsex==1: male=1elif sex==2: female=1else 3360 other=1return male、female、other bot=bot () other=get_friend_sex(friends ) total=len(friends(1: ) )打印结果打印('男性朋友: %.2f%%'%(float(male ) )

好啊,暴露了我男性朋友很多的真相~~~

似乎不直观,感兴趣的朋友可以加入可视化展示。 我这里使用的是matplotlib (有机会我会详细谈谈)。

直接打代码

fromwxpyimport* importmatplotlib.pyplotaspltdefget _ friend _ sex (friends ) : male=female=other=0foriinfriends 3360 sex=I.sexx 3360other=1returnmale,female,otherbot=bot(cache_path=true ) friends=bot.friends ) ) male,female, other=get_friend_sex(friends ) total=len ) friends[1:] ) my_name=friends(0).nick_name#每个部分名称sex other]#自动计算画布大小PLT.figure (fig size=(20,9 ),dpi=100 ) #饼图PLT.pie ) sex_counnt ) colors=['b '

at' % my_name)# 保证长宽一样plt.axis('equal')# 显示图像plt.show()

3、好友个性签名词云

主要是想看下朋友的个性签名中的高频词语

from wxpy import *bot = Bot(cache_path=True)friends = bot.friends()for i in friends: signature = i.signature.strip() print(signature)

先全部抓取下来
打印之后你会发现,有大量的span,class,emoji,emoji1f3c3等的字段,因为个性签名中使用了表情符号,这些字段都是要过滤掉的,写个正则过滤掉。

from wxpy import *import rebot = Bot(cache_path=True)friends = bot.friends()for i in friends: signature = i.signature.strip() rep = re.compile("<span.*emoji1fd.+</span>") signature = rep.sub("", signature) print(signature)

接来下用jieba分词,然后制作成词云,首先要安装jieba和wordcloud库

pip install jieba
pip install wordcloud
代码

import refrom wxpy import *import matplotlib.pyplot as pltfrom wordcloud import WordCloudimport jiebabot = Bot(cache_path=True)friends = bot.friends()tList = []for i in friends: signature = i.signature.strip() rep = re.compile("<span.*emoji1fd.+</span>") signature = rep.sub("", signature) tList.append(signature)# 拼接字符串text = "".join(tList)wordlist_jieba = jieba.cut(text, cut_all=True)wl_space_split = " ".join(wordlist_jieba)my_wordcloud = WordCloud(background_color="white", max_words=2000, max_font_size=40, random_state=42, font_path='c:\windows\Fonts\simhei.ttf').generate(wl_space_split)plt.figure(figsize=(40, 16), dpi=150)plt.imshow(my_wordcloud)plt.axis("off")plt.show()

运行代码

这。。好像有点丑,根据wordcloud用法,可以找一张图来生成配色方案,我这里找了一张小黄人的图片

修改一下代码

import refrom wxpy import *import matplotlib.pyplot as pltfrom wordcloud import WordCloud, ImageColorGeneratorimport numpy as npimport jiebaimport PIL.Image as Imageimport osbot = Bot(cache_path=True)friends = bot.friends()tList = []for i in friends: signature = i.signature.strip() rep = re.compile("<span.*emoji.+</span>") signature = rep.sub("", signature) tList.append(signature)# 拼接字符串text = "".join(tList)d = os.path.dirname(__file__)wordlist_jieba = jieba.cut(text, cut_all=True)wl_space_split = " ".join(wordlist_jieba)hsdxmy_coloring = np.array(Image.open(os.path.join(d, "wechat.jpg")))my_wordcloud = WordCloud(background_color="white", max_words=2000, mask=hsdxmy_coloring, max_font_size=40, random_state=42, font_path='c:\windows\Fonts\simhei.ttf').generate(wl_space_split)image_colors = ImageColorGenerator(hsdxmy_coloring)plt.figure(figsize=(40, 16), dpi=150)plt.imshow(my_wordcloud.recolor(color_func=image_colors))plt.imshow(my_wordcloud)plt.axis("off")plt.show()

然后还有微信自动回复,爬取表情包斗图,这些都是可以实现的,感兴趣的可以自行研究或者加我好友讨论一下。
个人微信:wukangcumt

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。