Python之第三方模块itchat

78 篇文章 3 订阅
24 篇文章 2 订阅

Python之第三方模块itchat

  • 什么是API

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

在linux中,用户编程接口API遵循了UNIX中最流行的应用编程界面标准—POSIX标准。POSIX标准是由IEEE和ISO/IEC共同开发的标准系统。该标准基于当时现有的UNIX实践和经验,描述了操作系统的系统调用编程接口API,用于保证应用程序可以在源程序一级上在多种操作系统上移植运行。这些系统调用编程接口主要是通过C库(LIBC)来实现的。

  • 制定ip地理位置的获取

网上有很多API接口, 直接可以返回json格式的数据, 通过处理, easy搞定……
根据IP查询所在地、运营商等信息的一些API如下:
1.淘宝的API:http://ip.taobao.com/service/getIpInfo.php?ip=110.84.0.129

2.新浪的API:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=110.84.0.129

3.腾讯的网页查询(返回的非json格式): http://ip.qq.com/cgi-bin/searchip?searchip1=110.84.0.129

4.ip.cn的网页(返回的非json格式):http://www.ip.cn/index.php?ip=110.84.0.129

5.ip-api.comhttp://ip-api.com/json/110.84.0.129

上述的API接口,大多有一个特点是, 返回的直接是个json格式;
Json数据: JavaScript Object Notation, 一种轻量型数据交换格式, 便于阅读和交换数据.

  • 什么是itchat模块

itchat是微信提供给python的一个接口包,其主要实现微信的基本功能,包括接发消息、获取好友个人资料等,要说有趣的东西,当然是注册图灵机器人,实现自动回复,其次还可以基于好友资料信息,做一些可视化的工作,包括签名的词云,性别的比例,好友的全国分布等

可自行下载了解:itchat
安装:

../bin/pip install itchat
  • itchat模块的应用示例

1.给手机助手发送消息

import itchat
import time
hotReload = True # 会保留登陆状态,在短时间内不用重新登陆
itchat.auto_login()
while True:
# 给微信手机助手发消息
itchat.send('hello',toUserName='filehelper')
itchat.send_file('/etc/passwd',toUserName='filehelper')
time.sleep(1)

2.统计微信的男女比例

import itchat
import time
hotReload = True 
itchat.auto_login()

iends = 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)

3.用图灵机器人工智能回复消息(需自行创建图灵机器人并复制api)

import itchat
import requests

def get_tuling_reponse(_info):
    print(_info)
    api_url = 'http://www.tuling123.com/openapi/api'
    data = {
        'key':'28a1d488a7fe47b5b637b750a6f3d66b',
        'info':_info,
        'userid':'haha'
    }
    # 发送数据到指定的网址,获取网址返回的数据
    res = requests.post(api_url,data).json()
    #print(res,type(res))
    # 给用户返回的内容
    print(res['text'])
    return (res['text'])

# get_tuling_reponse('给我讲个笑话')
# get_tuling_reponse('不好笑')

# 时刻监控好友发送的文本信息,并且给与一个回复
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def text_repky(msg):
    # 获取好友发送的文本信息
    # 返回文本信息
    content = msg['Content']
    # 将好友的消息发送给机器人去处理,处理的结果就是返回给好友的信息
    returnContent = get_tuling_reponse(content)
    return returnContent

itchat.auto_login()
itchat.run()

4.在微信端可使用linux系统命令

mport os
import itchat
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def text_reply(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()
itchat.run()

5.在python中执行shell命令

# 1.可以判断命令是否执行成功
# 返回值是0 执行成功
# 返回值不为0 执行不成功
import os
print(os.system('ls'))
res = os.system('hostnameeeee')
print(res)

# 2.用来保存命令的执行结果
res = os.popen('hostname').read()
print(res)
  • END
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值