使用wxpy自动发送微信消息

思路整理:1、进入心灵鸡汤网页,使用python获取心灵鸡汤内容

     2、登陆微信,找到需要发送的朋友

     3、发送获取的内容

1、获取心灵鸡汤的内容

  如下图,获取第一条鸡汤

  

  实现如下:

  

2、登陆微信,搜索朋友,进行发送

 1 import requests
 2 import wxpy
 3 from bs4 import BeautifulSoup
 4 
 5 # 微信网页登陆
 6 bot = wxpy.Bot(console_qr=2,cache_path='botoo.pkl')
 7 
 8 # 获取心灵鸡汤中的最新内容,可以参考其他爬虫随便查看怎么爬虫
 9 def get_msg():
10     url = 'http://www.59xihuan.cn/index_1.html'
11     h = requests.get(url)
12     html = h.text
13     news_bf = BeautifulSoup(html,"html.parser")
14     msg = news_bf.find('div', class_='pic_text1')
15     news = msg.text
16     # print(msg)
17     # print(news)
18     return news
19 
20 # 给朋友发送消息
21 def send_msg():
22     try:
23         # 添加朋友微信昵称
24         friend = bot.friends().search(u'xxxxx')[0]
25         friend.send(get_msg())
26         29     except:pass
30 
31 
32 if __name__ == '__main__':
33     send_msg()

其他发送类型格式:

  发送文本消息:friend.send('文本消息')
  发送图片消息:friend.send_image('图片消息.jpg')
  发送视频消息:friend.send_video('视频消息.mov')
  发送文件消息:friend.send_file('文件消息.zip')
  以动态的方式发送图片:friend.send('@img@图片消息.jpg')

朋友收到的消息:

  

有兴趣的可以参考笔者的爬取小说随笔,爬下心灵鸡汤的多条鸡汤和图片,然后发送朋友鸡汤和配图

如果恶搞的话可以去详细了解wxpy的使用,每秒发送朋友x条内容,持续y时间

原文地址:https://www.cnblogs.com/tynam/p/9106461.html