使用python调用微信的模块itchat

itchat是一个开源的微信个人号接口,使用python调用微信简单,使用起来也很容易。在大数据和人工智能非常火热的背景下,可以完成一个能够处理所有信息的微信机器人,具有时尚购物、家庭理财、学习教育等功能。以下是我简单写的代码,可以每隔一段时间给固定的群发送群消息。
通过此代码可以获得对应的群的userid。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# !/usr/bin/python
# coding:utf-8
# Python:   3.5.2
# Platform: Windows
# Author:   zll 
# Program:  Wechat1
# History:  2017/10/31 V1.0.0[Zll] 通过此代码可以获得对应的群的userid。
#           
import io
import sys
import itchat
 
itchat.auto_login(hotReload=True)
#获取好友的列表
friendslist = itchat.get_friends(update=True)[1:]
#获取群聊的列表,***代表的是你的微信里任一一个群聊的微信名称
itchat.get_chatrooms(update=True)[1:]
mpsList = itchat.search_chatrooms(name = '***')
chatroom=itchat.update_chatroom(mpsList[0]['UserName'])
#获取公众号的列表
publicsList = itchat.get_mps(update=True)[1:]
 
#将好友列表、群聊的列表、公众号的列表分别保存在test,test2,test3的文本文档中
f1=open('E:\\study\\Python\\script\\test\\test1.txt', 'w',encoding='utf-8')
f1.write(str(friendslist))
f1.close()
f2=open('E:\\study\\Python\\script\\test\\test2.txt', 'w',encoding='utf-8')
f2.write(str(chatroom))
f2.close()
f3=open('E:\\study\\Python\\script\\test\\test3.txt', 'w',encoding='utf-8')
f3.write(str(publicsList))
f3.close()

 

你可能感兴趣的:(脚本)