python中的第三方库,应用程序编程接口API

python第三方模块

import time 导入时间模块
import requests  requests是python实现的简单易用的HTTP库,被用于请求网站
time.sleep(5) 使系统睡眠5秒 
time.time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
time.ctime()函数把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。 如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于 asctime(localtime(secs))。


import math 导入数学模块
math.pi 输出的是pi的值
math.pow(2,3) 计算2的3次方
import os 导入操作系统模块

import random 导入随机数模块
import string 导入字母模块
random.random()  随机输出0~1之间的任意小数
random.randint(1,4)  随机输出1~4之间的任意整数
random.choice('hello')  随机选择其中的一个
random.sample(string.ascii_letters + string.digits,4) 随机从英文字母大小写和数字中任意选择4个.

random.shuffle(list(range(1,10))) 将列表随机排放

import functools
print(functools.reduce(lambda  x,y:x+y,range(10))) 将每次得到的结果作为初始值进行下一轮的运算.

API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。

微信编程接口(itchat)

import itchat
 hotReload=True,会保留登陆状态,在短时间内重新登陆不用再次扫描二维码
itchat.auto_login(hotReload=True)
 给手机助手发送消息
while True:
      itchat.send('hello',toUserName='filehelper')

统计微信好友的性别比例

import itchat
itchat.auto_login()
friends = itchat.get_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)

利用图灵机器人实现自动回复好友信息

import itchat
import requests

def get_tuling_reponse(_info):
 
    api_url = 'http://www.tuling123.com/openapi/api' #此处添加图灵机器人的网址
    data ={
        'key':'4d5fe42020fd489d88387c6a700698c8',  #图灵机器人的编码
        'info':_info,
        'userid':'帅帅凯专用机器人' #图灵机器人的名称
    }
    res = requests.post(api_url,data).json()  # 发送数据到指定网址,获取网址返回的数据
    return res['text']      给用户返回的内容
时刻监控好友发送的文本消息,并且给予一个回复
isGroupChat=True 接收群聊消息
isFriendChat=True 接收好友消息
注册响应事件,消息类型为itchat.content.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()
itchat.run()

向电脑发送命令,如果电脑执行成功,返回结果,如果失败,返回执行失败

import os
import itchat
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def text_reply(msg):
    """
    需求:当我们给文件助手发送消息的时候,执行发送的内容
        1.如果执行成功,显示执行结果
        2.如果执行失败,显示执行失败
    :param msg:
    :return:
    """
    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()
itchat.run()

利用微信给指定好友发送信息

import itchat
itchat.auto_login(hotReload=True)给指定好友发送消息

res = itchat.search_friends('小凯')  根据好友昵称查找好友的信息,返回值是一个列表,有多个元素
ly =  res[0]['UserName']      通过索引获取该好友的详细信息
while True:
    itchat.send('python',toUserName=ly)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值