python微信库:itchat

来源:互联网 发布:js地图api 定位 编辑:程序博客网 时间:2024/05/07 09:49

因为最近的项目,需要使用微信的接口获取一些数据。
参考:python实现微信接口(itchat)
安装:pip install itchat
常见接口使用:
import itchat
import pandas an pd
1、登录
itchat.login()
itchat.auto_login()
这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实短时间的登录,并不会保留登录的状态,也就是下次登录时还是需要扫描二维码,如果加上hotReload==True,那么就会保留登录的状态,至少在后面的几次登录过程中不会再次扫描二维码,该参数生成一个静态文件itchat.pkl用于存储登录状态
2、获取好友列表
itchat.get_friends(update=True)
3、导出好友列表
df_friends = pd.DataFrame(friends)
df_friends.to_csv(‘friends.csv’, encoding=’utf-8’,index = False)
发送消息
send(msg=”Text Message”, toUserName=None)
参数
msg : 文本消息内容
@fil@path_to_file : 发送文件
@img@path_to_img : 发送图片
@vid@path_to_video : 发送视频
toUserName : 发送对象, 如果留空, 将发送给自己.
返回值
True or False
实例代码

# coding-utf-8import itchatitchat.auto_login()itchat.send("Hello World!")ithcat.send("@fil@%s" % '/tmp/test.text')ithcat.send("@img@%s" % '/tmp/test.png')ithcat.send("@vid@%s" % '/tmp/test.mkv')

- send_msg

itchat.send_msg("hello world.")

- send_file

itchat.send_file("/tmp/test.txt")

-send_image

itchat.send_img("/tmp/test.txt")

- send_video

itchat.send_video("/tmp/test.txt")
原创粉丝点击