如何利用python之wxpy模块玩转微信


Posted in Python onAugust 17, 2020

wxpy也是一个python的模块,利用它我们可以做很多有意思的事情
首先利用一句代码我们就可以利用python登录网页版微信

bot = Bot(cache_path= True)

这条语句会产生一个二维码,我们扫描了这个二维码之后就可以登录我们的微信了
功能一:获得微信好友信息
利用一行语句获得你微信好友的个数、男女比例、TOP10省份及TOP10城市

my_friends.stats_text()

效果如图

如何利用python之wxpy模块玩转微信

利用下面两行代码我们可以给微信好友发送信息

friends = my_friends.search('你想要发送的人名')[0]
friends.send('你想要发送的信息')

所以衍生了下面两个功能
功能二:群发消息

my_friend = bot.friends()
for i in my_friend[1:]:
 a = i.name
 friend = my_friend.search(a)[0]
 print('正在发送',friend)
 friend.send('')#你想要发送的内容
 print('ok')
 time.sleep(1)#由于发送消息太快最后加上一个延迟

功能三:消息轰炸

friends = my_friends.search('你想要发送的人名')[0]
for i in range(50):
 friends.send('你想要发送的信息')

我这里是发了50遍,记得加上time.sleep(),要是发送太快会被禁止发信息的
功能四:获得好友头像
利用friend.get_avatar函数

def CREATE_PICPATHT():
 path = os.getcwd() +"\\pic\\"
 if not os.path.exists(path):
  os.mkdir(path)
  return path
def IMAGE_SAVE(path):
 my_friends = bot.friends()
 num = 0
 for friend in my_friends:
  print(friend.name)
  friend.get_avatar(path + '\\' + str(num) + ".jpg")
  num = num + 1
path = CREATE_PICPATHT()
IMAGE_SAVE(path)

效果如图:

如何利用python之wxpy模块玩转微信

功能五:头像拼接
下面展示一些 内联代码片

def PJ_IMAGE(path):
 length = len(os.listdir(path))
 image_size = 2560
 each_size = math.ceil(2560 / math.floor(math.sqrt(length)))
 x_lines = math.ceil(math.sqrt(length))
 y_lines = math.ceil(math.sqrt(length))
 image = Image.new('RGB', (each_size * x_lines, each_size * y_lines))
 x = 0
 y = 0
 for (root, dirs, files) in os.walk(path):
  for pic_name in files:
   try:
    with Image.open(path + pic_name) as img:
     img = img.resize((each_size, each_size))
     image.paste(img, (x * each_size, y * each_size))
     x += 1
     if x == x_lines:
      x = 0
      y += 1
   except IOError:
    print("头像读取失败")
  img = image.save(os.getcwd() +"/wechat.png")
  print('已完成')

path就是上面获得头像的path,这串代码是借鉴别的大神的

如何利用python之wxpy模块玩转微信

最后我把代码整合在了一起并加上了按钮和界面,如下图

如何利用python之wxpy模块玩转微信

输入的用户名可以是备注也可以是原名,然后群发的消息也是放在第二行点击一下就好了,好友信息会以txt的文件存放,好友图片会放在文件夹里,虽然亚子有点丑

如何利用python之wxpy模块玩转微信

最后我也打包成了exe文件,可以直接执行

如何利用python之wxpy模块玩转微信
如何利用python之wxpy模块玩转微信

最后附上完整代码
下面展示一些 内联代码片

from wxpy import *
import os
import tkinter as tk
import tkinter
import math
from PIL import Image
import time
window = tkinter.Tk()
window.title('微信')
window.geometry("800x480")
bot = Bot(cache_path= True)
l1 = tk.Label(window, text="第一行输入用户名第二行输入信息",
    font=("黑体", 10))
l1.pack()
ask_text = tk.Entry(background = 'orange')
ask_text.pack()
ask_text1 = tk.Entry(background = 'pink')
ask_text1.pack()
def onclick():
 a = ask_text.get()
 my_friends = bot.friends()
 friends = my_friends.search(a)
 return friends[0]
def onclick1():
 a = ask_text1.get()
 return a
def CREATE_PICPATHT():
 path = os.getcwd() +"\\pic\\"
 if not os.path.exists(path):
  os.mkdir(path)
  return path
def IMAGE_SAVE(path):
 my_friends = bot.friends()
 num = 0
 for friend in my_friends:
  print(friend.name)
  friend.get_avatar(path + '\\' + str(num) + ".jpg")
  num = num + 1
def CREATE_TXTPATH():
 a = os.getcwd()
 filename = a + '\用户信息' + '.txt'
 return filename
def GET_FriendSTXT(filenmame):
 my_friend = bot.friends()
 with open(filenmame,'w') as f:
  f.write(my_friend.stats_text())
 print('ok')
def SEARCH_FRIENDS(name):
 my_friends = bot.friends()
 friends = my_friends.search(name)
 return friends[0]
def SEND_MESSAGES(friends,message):
 friends.send(message)
def func():
 path = CREATE_TXTPATH()
 GET_FriendSTXT(path)
def func1():
 path = CREATE_PICPATHT()
 IMAGE_SAVE(path)
 PJ_IMAGE(path)
def func2():
 a = onclick()
 b = onclick1()
 a.send(b)
 print('发送成功')
def func3():
 for i in range(50):
  time.sleep(1)
  func2()
def PJ_IMAGE(path):
 length = len(os.listdir(path))
 image_size = 2560
 each_size = math.ceil(2560 / math.floor(math.sqrt(length)))
 x_lines = math.ceil(math.sqrt(length))
 y_lines = math.ceil(math.sqrt(length))
 image = Image.new('RGB', (each_size * x_lines, each_size * y_lines))
 x = 0
 y = 0
 for (root, dirs, files) in os.walk(path):
  for pic_name in files:
   try:
    with Image.open(path + pic_name) as img:
     img = img.resize((each_size, each_size))
     image.paste(img, (x * each_size, y * each_size))
     x += 1
     if x == x_lines:
      x = 0
      y += 1
   except IOError:
    print("头像读取失败")
  img = image.save(os.getcwd() +"/wechat.png")
  print('已完成')

def func4():
 my_friend = bot.friends()
 b = onclick1()
 for i in my_friend[1:]:
  a = i.name
  friend = my_friend.search(a)[0]
  print('正在发送', friend)
  friend.send(b) # 你想要发送的内容
  print('ok')
  time.sleep(1)
window.bind('<Return>', onclick)
click_button = tkinter.Button(window,
        text = '获取好友信息',
        background = 'purple',
        width = 10,
        height = 4,
        command = func)

click_button.pack(side = 'left')
click_button1 = tkinter.Button(window,
        text = '获取好友图片',
        background = 'green',
        width = 10,
        height = 4,
        command = func1)
click_button1.pack(side = 'right')
click_button2 = tkinter.Button(window,
        text = '点击发送信息',
        background = 'blue',
        width = 10,
        height = 4,
        command = func2)
click_button2.pack(side = 'top')
click_button3 = tkinter.Button(window,
        text ='连续发送五十',
        background = 'pink',
        width = 10,
        height = 4,
        command = func3)
click_button3.pack()
click_button4 = tkinter.Button(window,
        text ='群发信息',
        background = 'grey',
        width = 10,
        height = 4,
        command = func4)

click_button4.pack(side = 'bottom')
window.mainloop()

总结

到此这篇关于利用python之wxpy模块玩转微信的文章就介绍到这了,更多相关python wxpy模块玩转微信内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
Dec 03 Python
在Python中使用模块的教程
Apr 27 Python
解决python文件字符串转列表时遇到空行的问题
Jul 09 Python
用Pygal绘制直方图代码示例
Dec 07 Python
详解Python异常处理中的Finally else的功能
Dec 29 Python
利用python 更新ssh 远程代码 操作远程服务器的实现代码
Feb 08 Python
Python使用OpenCV进行标定
May 08 Python
django 自定义filter 判断if var in list的例子
Aug 20 Python
使用Python制作一个打字训练小工具
Oct 01 Python
python判断无向图环是否存在的示例
Nov 22 Python
推荐8款常用的Python GUI图形界面开发框架
Feb 23 Python
Django用户登录与注册系统的实现示例
Jun 03 Python
深入了解Python 方法之类方法 &amp; 静态方法
Aug 17 #Python
详解如何在PyCharm控制台中输出彩色文字和背景
Aug 17 #Python
python如何操作mysql
Aug 17 #Python
详解python 内存优化
Aug 17 #Python
浅谈如何使用python抓取网页中的动态数据实现
Aug 17 #Python
详解Python 中的容器 collections
Aug 17 #Python
Python 解析库json及jsonpath pickle的实现
Aug 17 #Python
You might like
全国FM电台频率大全 - 14 江西省
2020/03/11 无线电
php面向对象全攻略 (十二) 抽象方法和抽象类
2009/09/30 PHP
PHP+SQL 注入攻击的技术实现以及预防办法
2010/12/29 PHP
php配置php-fpm启动参数及配置详解
2013/11/04 PHP
ThinkPHP模板Switch标签用法示例
2014/06/30 PHP
Yii2创建多界面主题(Theme)的方法
2016/10/08 PHP
ThinkPHP框架结合Ajax实现用户名校验功能示例
2019/07/03 PHP
yii2.0框架数据库操作简单示例【添加,修改,删除,查询,打印等】
2020/04/13 PHP
js的闭包的一个示例说明
2008/11/18 Javascript
实现JavaScript中继承的三种方式
2009/10/16 Javascript
JS定义回车事件(实现代码)
2013/07/08 Javascript
jQuery操作Select的Option上下移动及移除添加等等
2013/11/18 Javascript
在HTML代码中使用JavaScript代码的例子
2014/10/16 Javascript
JavaScript字符串对象charAt方法入门实例(用于取得指定位置的字符)
2014/10/17 Javascript
JQuery基础语法小结
2015/02/27 Javascript
jQuery插件bxSlider实现响应式焦点图
2015/04/12 Javascript
Javascript中arguments用法实例分析
2015/06/13 Javascript
[04:05]TI9战队采访 - Natus Vincere
2019/08/22 DOTA
八大排序算法的Python实现
2021/01/28 Python
python实现支付宝当面付(扫码支付)功能
2018/05/30 Python
Python运行提示缺少模块问题解决方案
2020/04/02 Python
Pandas DataFrame求差集的示例代码
2020/12/13 Python
详解python3 GUI刷屏器(附源码)
2021/02/18 Python
基于CSS3实现立方体自转效果
2016/03/01 HTML / CSS
html5嵌入内容_动力节点Java学院整理
2017/07/07 HTML / CSS
html5 localStorage本地存储_动力节点Java学院整理
2017/07/06 HTML / CSS
浅谈Html5多线程开发之WebWorkers
2018/05/02 HTML / CSS
美国美妆网站:B-Glowing
2016/10/12 全球购物
英国50岁以上人群的交友网站:Ourtime
2018/03/28 全球购物
美国购买体育赛事门票网站:TicketCity
2019/03/06 全球购物
数控加工专业毕业生自荐信
2013/09/27 职场文书
搞笑征婚广告词
2014/03/17 职场文书
2014年检察院个人工作总结
2014/12/09 职场文书
答谢词范文
2015/01/05 职场文书
民间借贷纠纷答辩状
2015/08/03 职场文书
pytorch实现加载保存查看checkpoint文件
2022/07/15 Python