1. 程式人生 > 程式設計 >使用wxpy實現自動傳送微信訊息功能

使用wxpy實現自動傳送微信訊息功能

思路整理:1、進入心靈雞湯網頁,使用python獲取心靈雞湯內容

     2、登陸微信,找到需要傳送的朋友

     3、傳送獲取的內容

1、獲取心靈雞湯的內容

  如下圖,獲取第一條雞湯

使用wxpy實現自動傳送微信訊息功能

  實現如下:

使用wxpy實現自動傳送微信訊息功能

2、登陸微信,搜尋朋友,進行傳送

import requests
import wxpy
from bs4 import BeautifulSoup

# 微信網頁登陸
bot = wxpy.Bot(console_qr=2,cache_path='botoo.pkl')

# 獲取心靈雞湯中的最新內容,可以參考其他爬蟲隨便檢視怎麼爬蟲
def get_msg():
  url = 'http://www.59xihuan.cn/index_1.html'
  h = requests.get(url)
  html = h.text
  news_bf = BeautifulSoup(html,"html.parser")
  msg = news_bf.find('div',class_='pic_text1')
  news = msg.text
  # print(msg)
  # print(news)
  return news

# 給朋友傳送訊息
def send_msg():
  try:
    # 新增朋友微信暱稱
    friend = bot.friends().search(u'xxxxx')[0]
    friend.send(get_msg())
    29   except:pass
if __name__ == '__main__':
  send_msg()

其他傳送型別格式:

  •   傳送文字訊息:friend.send('文字訊息')
  •   傳送圖片訊息:friend.send_image('圖片訊息.jpg')
  •   傳送視訊訊息:friend.send_video('視訊訊息.mov')
  •   傳送檔案訊息:friend.send_file('檔案訊息.zip')
  •   以動態的方式傳送圖片:friend.send('@img@圖片訊息.jpg')

朋友收到的訊息:

  使用wxpy實現自動傳送微信訊息功能

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。