python3操作微信itchat实现发送图片

yipeiwu_com7年前Python基础

两个代码,一个是把python当微信操作,可以查看自己的好友信息,群信息等。还可以定时发送文本,文件等。效果就和你本人操作一样,没差别。

还有一个是连接你的微信和图灵机器人,别人和你说话可以自动回复(非常愚蠢)。

itchat.search_friends(name=r' ‘) 是找到某好友
itchat.get_chatrooms(update=True)是获得所有群。同类。

#coding=utf8
import itchat
itchat.auto_login(hotReload=True) #热启动你的微信
#itchat.run()
rooms=itchat.get_chatrooms(update=True)
for i in range(len(rooms)):
 print(rooms[i]) #查看你多有的群

room = itchat.search_friends(name=r' ') #这里输入你好友的名字或备注。
print(room)
userName = room[0]['UserName']
f="C:\文件/lh.jpg" #图片地址
try:
 itchat.send_image(f,toUserName=userName) 如果是其他文件可以直接send_file
 print("success")
except:
 print("fail")

二、首先去图灵机器人注册账号,查看api接口和key:

#coding=utf8
import requests
import itchat
def get_response(msg):
 apiUrl = 'http://www.tuling123.com/openapi/api'
 data = {
  'key' : '', #这里自行输入key
  'info' : msg,
  'userid' : '175007',  #这是我的账号
 }
 try:
  r = requests.post(apiUrl, data=data).json()
  return r.get('text')
 except:
  return "呵呵" #出问题就回复“呵呵”


@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
 defaultReply = 'I received: ' + msg['Text'] #一个默认回复
 reply = get_response(msg['Text']) 
 return reply or defaultReply

itchat.auto_login(hotReload=True) #热启动,不需要多次扫码登录
itchat.run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 不同方式读取文件速度不同的实例

1、按行读取较慢较耗时: srcFiles = open('inputFile.txt', 'r') for file_path in srcFiles: file_path...

Ubuntu下使用python读取doc和docx文档的内容方法

读取docx文档 使用的包是python-docx 1. 安装python-docx包 sudo pip install python-docx 2. 使用python-docx...

TensorFlow实现简单卷积神经网络

TensorFlow实现简单卷积神经网络

本文使用的数据集是MNIST,主要使用两个卷积层加一个全连接层构建的卷积神经网络。 先载入MNIST数据集(手写数字识别集),并创建默认的Interactive Session(在没有指...

numpy.ndarray 交换多维数组(矩阵)的行/列方法

如下所示: >> import numpy as np >> P = np.eye(3) >> P array([[ 1., 0., 0.],...

在Python中处理字符串之isdigit()方法的使用

 isdigit()方法检查字符串是否只包含数字(全由数字组成)。 语法 以下是isdigit()方法的语法: str.isdigit() 参数  &...