python實戰----向微信發送消息


前提:

  1、微信號

  2、pip install wxpy

  3、pip install requests

代碼:

from __future__ import unicode_literals        #python3.x以上版本把改行注釋即可
from threading import Timer
from wxpy import *
import requests


#bot = Bot()
bot = Bot(console_qr=2,cache_path="botoo.pkl")       #這里的二維碼是用像素的形式打印出來!,如果你在windows環境上運行,替換為  bot=Bot()


def get_news1():
  #獲取金山詞霸每日一句,英文和翻譯
    url = "http://open.iciba.com/dsapi/"
    r = requests.get(url)
    contents = r.json()['content']
    translation= r.json()['translation']
    return contents,translation

def send_news():
    try:
        my_friend = bot.friends().search(u'徒手敬歲月')[0]    #你朋友的微信名稱,不是備注,也不是微信帳號。
        my_friend.send(get_news1()[0])
        my_friend.send(get_news1()[1][5:])
        my_friend.send(u"心靈雞湯!")
        t = Timer(10, send_news)         
        t.start()
    except:
        my_friend = bot.friends().search('常念')[0]         #你的微信名稱,不是微信帳號。
        my_friend.send(u"今天消息發送失敗了")
        

    
if __name__ == "__main__":
    send_news()

 

關於wxpy模塊可以參考http://wxpy.readthedocs.io/zh/latest/bot.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM