• python 实现微信发送消息


    背景:利用Python来登入你个人的手机微信,之后向朋友发送消息,发送的消息可以来源于网页。下面的例子就是取得当前日元的汇率,之后发送自己的某一个朋友的手机上

    环境:Python3,JetBrains PyCharm Community Edition 2018.2.4 x64,window7

    实现的效果如下:

    汇率的消息来源于网页

    具体实现:

    import json
    from threading import Timer
    from wxpy import *
    import requests
    import urllib.parse
    
    def get_news():
        '''获取金山词霸每日一句'''
        url = 'http://open.iciba.com/dsapi'
        r = requests.get(url)
        content = r.json()['content']
        note = r.json()['note']
        return content, note
    
    def send_news():
        val = getRequest()
        mssage = u'今天的日元汇率为:%s' % val
        bot = Bot()  # 连接微信,会出现一个登陆微信的二维码
        try:
            # my_friends =bot.friends().search(u'XXX')[0]#这里是你微信好友的昵称
            friends = bot.friends()
            for i in friends:
                name = i
                if "XXX" == name.name:
                    print(name)
                else:
                    print(name)
                    break
            # my_friend.send(contents[0])
            # my_friend.send(contents[1])
            name.send(mssage)
            t = Timer(86400, send_news)  # 这里是一天发送一次,86400s = 24h
            t.start()
        except:
            my_friend = bot.friends().search('filehelper')[0]
            my_friend.send(u'今天消息发送失败了')
    
    def getRequest():
        _header = getHeader()
        _data = urllib.parse.urlencode(getData()).encode('utf-8')
        url = 'https://webapi.huilv.cc/api/trend/yaho'
        response = requests.post(url, data=_data, headers=_header)
    
        print(response.encoding)
        print(response.apparent_encoding)
        r = response.text
        result_text = json.loads(r, encoding='utf-8')
        val = result_text["obj"][len(result_text["obj"]) -1 ]["huilv"]
        return val
    
    def getHeader():
        header = {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Referer': 'https://www.huilv.cc/zoushitu?a=JPYCNY&time=d1',
            'Sec-Fetch-Mode': 'cors',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
        }
        return header
    
    def getData():
        paydata = {
            'pinzhong': 'JPYCNY',
            'longs': 'd1'
        }
        return paydata
    
    if __name__ == '__main__':
        get_news()
        send_news()
  • 相关阅读:
    实习感悟——从用户中来,到用户中去
    FineUI PK DWZ
    Java入门到精通——工具篇之Maven概述
    信息论的熵
    菜鸟学习Hibernate——一对多关系映射
    StyleCop学习笔记——默认的规则
    StyleCop学习笔记——自定义规则
    StyleCop学习笔记——初识StyleCop
    好博客收藏
    菜鸟学习Hibernate——简单的增、删、改、查操作
  • 原文地址:https://www.cnblogs.com/killclock048/p/11384184.html
Copyright © 2020-2023  润新知