基于WxPusher实现微信消息推送
生活随笔
收集整理的這篇文章主要介紹了
基于WxPusher实现微信消息推送
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? 最近找到一個公眾號(WxPusher消息推送平臺),提供了api接口和開發文檔,可以借助python程序實現每日的消息推送,通過發送post請求,用戶只需要掃描二維碼就能實現推送的功能,并且支持群發。詳細如下:
? ? ? ? 這里將每日的天氣和一些文案信息爬取出來整合成字符串作為信息加入到待推送的信息中。
? ? ? ? 1、將要爬取的天氣等信息整理成字符并封裝成函數作為返回值返回,方便后面調用。
# 獲取信息 def get_info():url1 = '' # 要爬取的天氣預報網站(此處抹去)url2 = '' # 文案網站(此處抹去)header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) ''AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}response2 = requests.get(url2, headers=header) # 發送網絡請求response2.encoding = 'utf-8' # 編碼html2 = etree.HTML(response2.text) # 解析html字符串global numberdiv_all2s = html2.xpath('.//div[@class="mbox fl"]//div[@class="content"]/p[{page}]//text()'.format(page=number))div_all2 = processing(div_all2s)copywritting = div_all2response1 = requests.get(url1,headers=header) # 發送網絡請求response1.encoding = 'utf-8' # 編碼html1 = etree.HTML(response1.text) # 解析html字符串div_all1 = html1.xpath('//dl[@class="weather_info"]')for div in div_all1:names = div.xpath('.//a/@title')name = processing(names)areas = div.xpath('.//dd[@class="name"]//h1/text()')area = processing(areas)name = '今日' + nametimes=div.xpath('.//dd[@class="week"]/text()')time=processing(times)brief_times = time.split('日')[0]brief_time = processing(brief_times)Ts = div.xpath('.//dd[@class="weather"]//span//text()')T = processing(Ts)humid_1 = processing(div.xpath('.//dd[@class="shidu"]/b[1]//text()'))humid_2 = processing(div.xpath('.//dd[@class="shidu"]/b[2]//text()'))humid_3 = processing(div.xpath('.//dd[@class="shidu"]/b[3]//text()'))humid = humid_1 + '\n' + humid_2 + '\n' + humid_3air_qualitys = div.xpath('.//dd[@class="kongqi"]/h5/text()')air_quality = processing(air_qualitys)pms = div.xpath('.//dd[@class="kongqi"]/h6/text()')pm = processing(pms)suns = div.xpath('.//dd[@class="kongqi"]/span/text()')sun = processing(suns)cartoon = """へ /|/\7 ∠_// │ / /│ Z _,< / /`ヽ│ ヽ / 〉Y ` / /イ● 、 ● ??〈 /() へ | \〈>ー 、_ ィ │ /// へ / ノ<| \\ヽ_ノ (_/ │//7 |/>―r ̄ ̄`ー―_6"""weather_condition = (name + ': \n' +'時間: ' + time + '\n\n' + '溫度: ' + T + '\n\n' + humid + '\n' + air_quality + '\n'+ pm + '\n' + sun + '\n\n' + cartoon)global summarysummary = brief_time + '日' + area + ':' + '\n\n' + ' ! ' + T + ' !' + '\n\n' + sun + '\n\n' + copywrittingreturn (weather_condition)? ? ? ? 2、發送post請求,將要推送的信息傳遞出去。
url='http://wxpusher.zjiecode.com/api/send/message'headers = {"content-type":"application/json"}dat={"appToken":"AT_7zKfBalcmZEPARaha0guEr5crjsrVOZh","content":get_info(), # 調用函數,獲取推送的內容"summary":summary,#消息摘要,顯示在微信聊天頁面或者模版消息卡片上,限制長度100,可以不傳,不傳默認截取content前面的內容。"contentType":1,#內容類型 1表示文字 2表示html(只發送body標簽內部的數據即可,不包括body標簽) 3表示markdown"topicIds":[7826 #發送目標的topicId,是一個數組!!!,也就是群發,使用uids單發的時候, 可以不傳。],"uids":[#'UID_GOgrMd1Ad1FEEpRdbY41jl9PGY1x','UID_hBw1gAtLkne1INAQjyy2qS9iyO8o'],"url":"https://www.csdn.net/", #原文鏈接,可選參數#"verifyPay": false #是否驗證訂閱時間,true表示只推送給付費訂閱用戶,false表示推送的時候,不驗證付費,不驗證用戶訂閱到期時間,用戶訂閱過期了,也能收到。 } #post請求,發送的數據必須放在字典中,通過data參數傳遞出去 resp=requests.post(url,json=dat,headers=headers)? ? ? ? ? 3、實現效果:
? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ?
?
????????這里附上WxPusher的官方開發文檔,上面有詳細操作,有興趣的小伙伴可以嘗試,創作不易,希望大家多多支持。?
WxPusher 開發文檔:https://wxpusher.zjiecode.com/docs/#/
?
總結
以上是生活随笔為你收集整理的基于WxPusher实现微信消息推送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【ubuntu安装winehq】
- 下一篇: 美国公布长达35页的《2016-2045