微信程序化Talk之Wxpy

Python

自从学了Python后,觉得代码啥都能干(除了生孩子,o(∩_∩)o )。

1. 调用shell操纵电脑上的各种软件:

import win32api
app= application.Application().start(r'notepad.exe C:\Users\admin\Desktop\Python Code\Test_code1\tmp.txt')

2. 网站爬虫:

# -*- coding:utf-8 -*-
import requests
url = 'https://www.dy2018.com/html/gndy/dyzz/index.html'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
headers = {'User-Agent': user_agent}
r = requests.get(url)
html = requests.get(url, headers=headers).text
html = html.encode(r.encoding)
html = html.decode("gbk")
print html

3. 发送电子邮件:

# coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
from email import encoders
# 设置smtplib所需的参数
# 下面的发件人,收件人是用于邮件传输的。
smtpserver = 'smtp.163.com'
username = '******@163.com'
password = '******'#授权码
sender = '******@163.com'
# 收件人为多个收件人
receiver = ['******@qq.com']
subject = 'Python email test'
# 通过Header对象编码的文本,包含utf-8编码信息和Base64编码信息。
# 构造邮件对象MIMEMultipart对象
# 下面的主题,发件人,收件人,日期是显示在邮件页面上的。
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
# msg['From'] = '******@163.com <******@163.com>'username + " " + '<' + sender + '>'
msg['From'] = username + " " + '<' + sender + '>'
# 收件人为多个收件人,通过join将列表转换为以;为间隔的字符串
msg['To'] = ";".join(receiver)
# 构造文字内容
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com"
text_plain = MIMEText(text, 'plain', 'utf-8')
msg.attach(text_plain)
# 构造图片链接
filepath=r'C:\Users\admin\Desktop\Python Code\Test_code1\jpg\副本.jpg'
sendimagefile = open(filepath.decode('utf-8'), 'rb').read()
image = MIMEImage(sendimagefile)
filename="test.jpg"
image.add_header("Content-Disposition", "attachment", filename=('gbk', '', filename))
msg.attach(image)
# 构造附件
filepath=r'C:\Users\admin\Desktop\test.pdf'
sendfile = open(filepath.decode('utf-8'), 'rb').read()
text_att = MIMEText(sendfile, 'base64', 'gbk')
text_att["Content-Type"] = 'application/octet-stream'
filename="测试.pdf"
# 附件名称为中文时的写法
text_att.add_header("Content-Disposition", "attachment", filename=('gbk', '', filename))
msg.attach(text_att)
# 发送邮件
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
# 我们用set_debuglevel(1)就可以打印出和SMTP服务器交互的所有信息。
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

列举以上几个功能后,我又发现了一个好玩的Python模块-wxpy。

wxpy介绍:https://wxpy.readthedocs.io/zh/latest/index.html
在cmd中安装wxpy:pip install -U wxpy -i "https://pypi.doubanio.com/simple/"


然后一个简单示例(先导入wxpy模块;然后实例化Bot对象并保存缓存,这样做能在后面的程序调试中在手机上确认,而不必每次都扫描二维码,更方便些;最后给file_helper,也就是文件传输助手发一条信息):

# -*- coding:utf-8 -*-
from wxpy import *
bot = Bot(cache_path=True)
#bot = Bot()
bot.file_helper.send("hello, I'm a robot!")
给文件传输助手发一条消息!

接下来我们将实现给指定好友发送文字图片,并实现用图灵机器人与好友聊天等基本功能。


代码视频展示.gif

实时图灵机器人聊天视频.gif

最后po出完整代码,感觉挺好玩的。有时候也可以编写程序实时推送关键新闻或者天气给自己。还可以在某些大型项目的结尾加上wxpy模块调用微信,结束的时候将仿真结果或者图片规范化,给自己发一条微信。甚至可以让对面的微信能在每一次仿真结束后都询问并接收你发出的新指令,进行变量修改后再继续建模仿真调试。

# -*- coding:utf-8 -*-
from wxpy import *
bot = Bot(cache_path=True)
# 查找到要使用机器人来聊天的好友
my_friend = (bot.search(u'花花'))
print my_friend
number=int(raw_input("需要选择第几个?请输入:"))
my_friend=my_friend[number-1]
# 发送文本
my_friend.send('Hello,  I am a robot!')
# 发送图片
my_friend.send_image('test.jpg')
# 发送文件
my_friend.send_file('test.txt')

my_friend.send('Tuling robot API')
# 调用图灵机器人API
tuling = Tuling(api_key='4a0488cdce684468b95591a641f0971d')
# 使用图灵机器人自动与指定好友聊天
@bot.register(my_friend)
def reply_my_friend(msg):
    tuling.do_reply(msg)
embed()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    小迈克阅读 8,133评论 1 3
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,652评论 6 427
  • 英文原版:https://github.com/vinta/awesome-python中文版:https://g...
    会灰的大飞狼阅读 8,940评论 1 56
  • Python资源大全中文版,包括:Web框架、网络爬虫、模板引擎、数据库、数据可视化、图片处理等,由伯乐在线持续更...
    dxl1236阅读 10,138评论 2 33
  • 蜡烛爆炸 吹灭山火 冰冷的喊声 荒唐涌动 欲望和自由 撕扯牢房 挖出罪犯的眼睛 嵌在石像的额头 从此石像有了生命 ...
    狐夕阅读 2,607评论 2 0