使用qrcode模块如何生成二维码
# 执行以下命令安装,我这里已经安装所以提示already
pip3 install qrcode
代码如下
import qrcode
img = qrcode.make('http://www.baidu.com')
img.save('hello.png')
执行完成没有报错就会在当前目录下生成一个图片文件hello.png,打开后就是我们生成的二维码
如果提示缺少模块PIL错误那就下载PIL模块
PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用。
由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又加入了许多新特性,因此,我们可以直接安装使用Pillow。
我这里安装的是python3.6,使用pip3下载,我这里已经配置好环境变量,没有配置好的同学们,可以直接cd到你们python3安装目录下的二进制文件直接执行./pip3 install Pillow
pip3 install Pillow
python实现微信接口————itchat模块
import itchat
import time
import random
# 实现扫码微信临时登陆,加入hotReload=True参数可以保留一段时间,该参数生成一个静态文件itchat.pk1用于存储登陆状态
itchat.auto_login(hotReload=True)
#1.实现发送消息
while True:
# 发送消息或者文件给微信文件传输助手
# 发送消息
itchat.send('hello', toUserName='filehelper')
# 发送文件
itchat.send_file('/etc/passwd', toUserName='filehelper')
time.sleep(random.randint(1, 3))
# 2.统计男女以及其他人数
# itchat的get_friends方法获取好友信息
friends = itchat.get_friends()
# print(friends)
info = {}
for friend in friends[1:]:
if friend['Sex'] == 1:
info['male'] = info.get('male', 0) + 1
elif friend['Sex'] == 2:
info['female'] = info.get('female', 0) + 1
else:
info['other'] = info.get('other', 0) + 1
print(info)
"""
需求:给文件助手发送消息,执行发送的内容
1.如果执行成功,现实执行结果
2.如果执行失败,现实执行失败
"""
import itchat
import os
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
if msg['ToUserName'] == 'filehelper':
# 获取要执行命令的内容
command = msg['Content']
print(command)
# 让电脑执行命令代码
# 如果执行成功,返回值为0
if os.system(command) == 0:
res = os.popen(command).read()
result = '【返回值】- 命令执行成功,执行结果:\n' + res
itchat.send(result, 'filehelper')
# 命令执行失败,请重新输入命令
else:
result = '【返回值】- 命令%s执行失败,请重试' % (command)
itchat.send(result, 'filehelper')
itchat.auto_login()
itchat.run()