本文實例講述了python實現的自動發送消息功能。分享給大家供大家參考,具體如下:
一個簡單的腳本
#-*- coding:utf-8 -*-
from __future__ import unicode_literals
from threading import Timer
import itchat
import requests
# 抓取金山毒霸每日一句,英文和翻譯
def get_news():
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
content = r.json()['content']
translation = r.json()['translation']
return content, translation
def send_news():
try:
# 把抓取的數據傳參
contents = get_news()
# 登陸微信賬戶,掃碼登陸
itchat.auto_login(hotReload=True)
# 查找你微信號上想要發送人的名稱
my_friend = itchat.search_friends(name=u'loli')
# 發送消息
itchat.send(contents[0], toUserName=my_friend)
itchat.send(contents[1], toUserName=my_friend)
itchat.send(u"I love you", toUserName=my_friend)
# 一天循環發送一次
t = Timer(86400, send_news)
t.start()
except:
my_friend = itchat.search_friends(name=u'loli')
itchat.send(u"今天消息發送失敗了", toUserName=my_friend)
if __name__ == '__main__':
send_news()
需要安裝的包:
pip install requests
pip install itchat
這里以抓取金山毒霸句子為例。
效果圖:
參考資料:
https://zhuanlan.zhihu.com/p/36398109
https://www.jb51.net/article/136992.htm
更多關于Python相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

