python—模块应用

模块应用中,选择选择python3.x版本

生成二维码

import qrcode
img = qrcode.make(‘http://www.baidu.com‘)
img.save(‘hello.png’)

统计微信好友列表男女比例

import itchat
import random
import time

itchat.auto_login(hotReload=True)
fd = itchat.get_friends()
info = {}
for f in fd[1:]:
if f[‘Sex’] == 1:
info[‘male’] = info.get(‘male’, 0) + 1
elif f[‘Sex’] == 2:
info[‘female’] = info.get(‘female’, 0) + 1
else:
info[‘other’] = info.get(‘other’, 0) + 1
print(info)

微信聊天小助手

在编写这个程序之前,首先在浏览器访问www.tuling123.com创建聊天机器人,
import itchat
import requests
import time

def get_tuling_reponse(_info):
print(_info)
api_url = ‘http://www.tuling123.com/openapi/api’ #api地址
data = {
‘key’: ‘77a131268fa548caa39f0966546a0fa3’, #网页自动生成的key值
‘info’:_info,
‘userid’: ‘benben’ #机器人名称
}
发送数据到指定的网址,获取网址返回的数据(字典数据类型)
res =requests.post(api_url,data).json()
# 返回的内容
print(res[‘text’])
return res[‘text’]
# 时刻监控好友发送的文本信息,并且给予一个回复
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def text_reply(msg):
content=msg[‘Content’]
returnContent=get_tuling_reponse(content)
return returnContent
itchat.auto_login(hotReload=True)
itchat.run()

微信控制linux

import os
import itchat

# 在python中执行shell命令
# 1.可以判断命令是否执行成功;如果返回值为0,则执行成功 如果不为0 则执行不成功
# res = os.system(‘hostname’)
# print(res)

# 2.保存命令的执行结果
# res = os.popen(‘hostname’).read()
# print(res)

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
“””
需求:当文件助手发送消息的时候,执行发送的内容
1.如果执行成功,显示执行成功:执行结果
2.反之,则显示失败
“””
print(msg)
if msg[‘ToUserName’] == ‘filehelper’:
# 获取要执行命令的内容
command = msg[‘Content’]
# 让电脑执行命令代码
# 如果执行成功,返回值是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(hotReload=True)
itchat.run()

猜你喜欢

转载自blog.csdn.net/weixin_42915309/article/details/82461818
今日推荐