Python3 實現(wxpy)用微信自動定時給朋友定時推廣


Python3 實現(wxpy)用微信自動定時給朋友定時推廣

安裝wxpy

安裝微信機器人wxpy非常簡單,如果你擁有pip,請直接按照Github中的方法安裝:
pip install wxpy

實例代碼

'''
微信:
Date:20180918
Author:lizm
Description:給指定朋友定時發送信息
'''
from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests

# 初始化機器人,掃碼登陸
bot = Bot()
#bot = Bot(console_qr=2,cache_path="botoo.pkl") #這里的二維碼是用像素的形式打印出來!

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"來自lee的心靈雞湯!")
        t = Timer(86400, send_news) #每86400秒(1天),發送1次,不用linux的定時任務是因為每次登陸都需要掃描二維碼登陸,很麻煩的一件事,就讓他一直掛着吧
        t.start()
    except:
        my_friend = bot.friends().search('好友微信名稱')[0] #你的微信名稱,不是微信帳號。
        my_friend.send(u"今天消息發送失敗了")

if __name__ == "__main__":
    send_news()

 

 


免責聲明!

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



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