欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实现微信向多好友定时发送信息

程序员文章站 2022-10-04 09:03:05
@TOC利用WeChat向好友定时发送关心及天气信息,表达自己对朋友的关心第一篇博客,不喜欢不要说出来哈_在闲暇之余,通过学习整理了一份可以向微信好友批量发送消息的脚本,包含的内容如下:1、获取天气信息2、获取关心数据3、好友配置表4、实现微信发送5、实现定时发送6、等等看了很多有帮助的博客1、python 每天在指定时间段运行程序及关闭程序https://blog.csdn.net/weixin_35737303/article/details/99561792?ops_reques...

@TOC利用WeChat向好友定时发送关心及天气信息,表达自己对朋友的关心

第一篇博客,不喜欢不要说出来哈_

在闲暇之余,通过学习整理了一份可以向微信好友批量发送消息的脚本,包含的内容如下:
1、获取天气信息
2、获取关心数据
3、好友配置表
4、实现微信发送
5、实现定时发送
6、等等

看了很多有帮助的博客

1、python 每天在指定时间段运行程序及关闭程序
https://blog.csdn.net/weixin_35737303/article/details/99561792?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159488554919725247647094%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=159488554919725247647094&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v3~pc_rank_v3-1-99561792.pc_ecpm_v3_pc_rank_v3&utm_term=python%E6%8C%87%E5%AE%9A%E6%97%B6%E9%97%B4%E6%89%A7%E8%A1%8C%E7%A8%8B%E5%BA%8F

2、写一个python定时发送消息的脚本——每天跟你女盆友说晚安(没有女朋友,说不定可以有哟)https://blog.csdn.net/weixin_41048363/article/details/79679987

@[TOC] 正文

现在进入正文

最终效果图展示

实现微信向多好友定时发送信息

获取天气&关心消息

费话不多说,先上代码,共有3个文件,两个.py(一个获取信息,一个发送信息) ,一个.json

获取信息.py

下面展示一些 内联代码片

// A code block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
def geturltext(xingming,html):#获取天气情况
    r = requests.get(html)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    tx = r.text
    aim = re.findall(r'<input type="hidden" id="hidden_title" value="(.*?)月(.*?)日(.*?)时(.*?) (.*?)  (.*?)  (.*?)"', tx)
    # airdata = re.findall(r'<li class="li6">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>', tx)
    ult_index = re.findall(r'<li class="li1">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</li>', tx)
    cloth_index = re.findall(r'<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</a>\n</li>\n<li class="li4">', tx)
    wash_index = re.findall(r'<li class="li4">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>', tx)
    txt1 = '[爱心]->看一看'+xingming+'的天气预报[爱心]'+'\n'+getYMD()+'\n'
    txt2 = '天气情况: '+aim[0][5]+'\n'+'温度情况: '+aim[0][6]+'\n'
    # txt3 = '空气指数: '+airdata[0][0]+', '+airdata[0][2]+'\n'
    txt4 = '紫外线指数: '+ult_index[0][0]+', '+ult_index[0][2]+'\n'
    txt5 = '穿衣指数: '+cloth_index[0][0]+', '+cloth_index[0][2]+'\n'
    txt6 = '洗车指数: '+wash_index[0][0]+', '+wash_index[0][2]+'\n'
    txt = '\n'+txt1+txt2+txt4+txt5+txt6
    return txt

关心消息

// A code block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
def get_iciba_everyday_chicken_soup(xingming):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    all = json.loads(r.text)
    Englis = all['content']
    Chinese = all['note']
    everyday_soup = '[玫瑰]->喝一碗'+xingming+'的心灵鸡汤[玫瑰]'+'\n'+Englis+'\n'+Chinese+'\n'
    return everyday_soup

突发奇想写的一个装饰文本消息的方法(丑爆了)

// A code block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
def zhuangshi():
    ic = '[怄火][怄火][怄火][怄火][怄火][怄火][怄火][怄火][怄火][怄火][怄火]'
    ic1 = '[玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰]'
    ic2 = '[爱心][爱心][爱心][爱心][爱心][爱心][爱心][爱心][爱心][爱心][爱心]'
    return ic+ic1+ic2

获取得系统日历信息

// A code block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
def getYMD():#获得对应的农历
    ymc = [u"十一", u"十二", u"正", u"二", u"三", u"四", u"五", u"六", u"七", u"八", u"九", u"十"]
    rmc = [u"初一", u"初二", u"初三", u"初四", u"初五", u"初六", u"初七", u"初八", u"初九", u"初十",
           u"十一", u"十二", u"十三", u"十四", u"十五", u"十六", u"十七", u"十八", u"十九",
           u"二十", u"廿一", u"廿二", u"廿三", u"廿四", u"廿五", u"廿六", u"廿七", u"廿八", u"廿九", u"三十", u"卅一"]
    Gan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
    Zhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
    ShX = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
    numCn = ["天", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
    lunar = sxtwl.Lunar()
    year = datetime.datetime.now().year
    month = datetime.datetime.now().month
    rday = datetime.datetime.now().day
    day = lunar.getDayBySolar(year, month, rday)
    d = str(day.y) + "年" + str(day.m) + "月" + str(day.d) + "日"
    if day.Lleap:
        a = "润" + ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日"
    else:
        a = ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日"
    b = "星期" + numCn[day.week]
    c = Gan[day.Lyear2.tg] + Zhi[day.Lyear2.dz] + "年" + Gan[day.Lmonth2.tg] + Zhi[day.Lmonth2.dz] + "月" + Gan[
        day.Lday2.tg] + Zhi[day.Lday2.dz] + "日"
    txt = '今天日期:'+d + ', ' + b + '\n'+'中华农历: ' + a + ', ' + c
    return txt

向另一个py爆露了一个接口

// A code block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
#封装  以参数形式获取指定信息,爆露给其他程序
def informationInterfaceParmer(callName,cityPoztion):
    code = getURLcode(cityPoztion)
    html = getURL(code)
    text = get_iciba_everyday_chicken_soup(callName) + geturltext(callName, html)

    # resault = zhuangshi()+text+zhuangshi()
    # print(resault)
    return zhuangshi()+""+text+""+zhuangshi()

附上获取信息全部代码

// An highlighted block
var foo = 'bar';
import json,re,datetime
import requests,itchat,sxtwl
import os,sys

def zhuangshi():
    ic = '[怄火][怄火][怄火][怄火][怄火][怄火][怄火][怄火][怄火][怄火][怄火]'
    ic1 = '[玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰][玫瑰]'
    ic2 = '[爱心][爱心][爱心][爱心][爱心][爱心][爱心][爱心][爱心][爱心][爱心]'
    return ic+ic1+ic2


def getYMD():#获得对应的农历
    ymc = [u"十一", u"十二", u"正", u"二", u"三", u"四", u"五", u"六", u"七", u"八", u"九", u"十"]
    rmc = [u"初一", u"初二", u"初三", u"初四", u"初五", u"初六", u"初七", u"初八", u"初九", u"初十",
           u"十一", u"十二", u"十三", u"十四", u"十五", u"十六", u"十七", u"十八", u"十九",
           u"二十", u"廿一", u"廿二", u"廿三", u"廿四", u"廿五", u"廿六", u"廿七", u"廿八", u"廿九", u"三十", u"卅一"]
    Gan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
    Zhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
    ShX = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
    numCn = ["天", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
    lunar = sxtwl.Lunar()
    year = datetime.datetime.now().year
    month = datetime.datetime.now().month
    rday = datetime.datetime.now().day
    day = lunar.getDayBySolar(year, month, rday)
    d = str(day.y) + "年" + str(day.m) + "月" + str(day.d) + "日"
    if day.Lleap:
        a = "润" + ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日"
    else:
        a = ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日"
    b = "星期" + numCn[day.week]
    c = Gan[day.Lyear2.tg] + Zhi[day.Lyear2.dz] + "年" + Gan[day.Lmonth2.tg] + Zhi[day.Lmonth2.dz] + "月" + Gan[
        day.Lday2.tg] + Zhi[day.Lday2.dz] + "日"
    txt = '今天日期:'+d + ', ' + b + '\n'+'中华农历: ' + a + ', ' + c
    return txt

def get_iciba_everyday_chicken_soup(xingming):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    all = json.loads(r.text)
    Englis = all['content']
    Chinese = all['note']
    everyday_soup = '[玫瑰]->喝一碗'+xingming+'的心灵鸡汤[玫瑰]'+'\n'+Englis+'\n'+Chinese+'\n'
    return everyday_soup

def getURLcode(location):#获取城市对应的代码
    url = "http://www.weather.com.cn/forecast/"
    r = requests.get(url)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    txt = r.text
    loc = re.findall(r' target="_blank">[\u4e00-\u9fa5]{1,5}?</a></li>', txt)
    cod = re.findall(r' <li><a href="http://www.weather.com.cn/weather1d/[0-9]{9}.shtml"', txt)
    loca, loca_1, code, code_1 = [], [], [], []
    a_cdict = {}
    for item in loc:
        loca.append(re.findall(r'target="_blank">(.*?)</a></li>', item))
    for i in range(len(loca)):
        loca_1.append(loca[i][0])
    loca_1.insert(47, '固原')#这个城市没有爬取到
    for item in cod:
        code.append(re.findall(r'[0-9]{9}', item))
    for i in range(len(code)):
        code_1.append(code[i][0])
    a_cdict = dict(zip(loca_1, code_1))
    return str(a_cdict[location])

def getURL(code):#获取城市对应的链接
    url = 'http://www.weather.com.cn/weather/'+code+'.shtml'
    return url

def geturltext(xingming,html):#获取天气情况
    r = requests.get(html)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    tx = r.text
    aim = re.findall(r'<input type="hidden" id="hidden_title" value="(.*?)月(.*?)日(.*?)时(.*?) (.*?)  (.*?)  (.*?)"', tx)
    # airdata = re.findall(r'<li class="li6">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>', tx)
    ult_index = re.findall(r'<li class="li1">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</li>', tx)
    cloth_index = re.findall(r'<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</a>\n</li>\n<li class="li4">', tx)
    wash_index = re.findall(r'<li class="li4">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>', tx)
    txt1 = '[爱心]->看一看'+xingming+'的天气预报[爱心]'+'\n'+getYMD()+'\n'
    txt2 = '天气情况: '+aim[0][5]+'\n'+'温度情况: '+aim[0][6]+'\n'
    # txt3 = '空气指数: '+airdata[0][0]+', '+airdata[0][2]+'\n'
    txt4 = '紫外线指数: '+ult_index[0][0]+', '+ult_index[0][2]+'\n'
    txt5 = '穿衣指数: '+cloth_index[0][0]+', '+cloth_index[0][2]+'\n'
    txt6 = '洗车指数: '+wash_index[0][0]+', '+wash_index[0][2]+'\n'
    txt = '\n'+txt1+txt2+txt4+txt5+txt6
    return txt

def sentauto(nicheng,txt):
    itchat.login()
    result = itchat.search_friends(nickName=nicheng)
    user_name = result[0]['UserName']
    itchat.send_msg(txt, user_name)


#封装  获取指定信息,爆露给其他程序
#默认使用,自己调用
def informationInterface():
    code = getURLcode("北京")
    html = getURL(code)
    text = get_iciba_everyday_chicken_soup("哥") + geturltext("哥", html)

    # resault = zhuangshi()+text+zhuangshi()
    # print(resault)
    return zhuangshi()+""+text+""+zhuangshi()


#封装  以参数形式获取指定信息,爆露给其他程序
def informationInterfaceParmer(callName,cityPoztion):
    code = getURLcode(cityPoztion)
    html = getURL(code)
    text = get_iciba_everyday_chicken_soup(callName) + geturltext(callName, html)

    # resault = zhuangshi()+text+zhuangshi()
    # print(resault)
    return zhuangshi()+""+text+""+zhuangshi()



if __name__ =="__main__":
    informationInterface()

配置文件格式(friend.json)

注意(waring):
1、一定是昵称不能是微信号
2、城市只写名字就好,不要带”市“字,例如:北京市,就写北京
{
“friend1”:{“nickName”:“微信好友昵称”,“cityPoztion”:“所在城市”,“callName”:“与好友关系”}
}

发送.py

导入所以需要的库

// An highlighted block
var foo = 'bar';
from __future__ import                        
import threading       
from datetime import ti
import datetime as dt  
from time import sleep 
from wxpy import *     
import json            
import window          
import multiprocessing 

登录微信

// An highlighted block
var foo = 'bar';
bot = Bot(console_qr=2,cache_path='botoo.pk1')
# bot = Bot(console_qr=True,cache_path=True)  

设置程序在那个时间段可以运行

// An highlighted block
var foo = 'bar';
# 程序运行时间在白天7:477:58  晚上 20012004
DAY_START = time(7, 47)   
DAY_END = time(7, 58)     
NIGHT_START = time(20,1)  
NIGHT_END = time(20, 4)   

获取消息文件内的信息

// An highlighted block
var foo = 'bar';
def get_news():                                                                                     
    # url = "http://open.iciba.com/dsapi"                                                           
    # r = requests.get(url)                                                                         
    # contents = r.json()['content']                                                                
    # translation = r.json()['translation']                                                         
    contents = window.informationInterface()   #调用window模块攻取关心数据                                    
    print(contents)                                                                                 
    return contents                                                                                 
                                                                                                    

向好友发送消息

// An highlighted block
var foo = 'bar';
def send_news(nickName,message):                                                              
    global t                                                                                  
    try:                                                                                      
        mfriend = bot.friends().search(nickName)[0]  #朋友昵称   半夏おレ记忆    Christine 곽걸 洁儿        
        # mfriend.send(get_news()[0])      #会发送多于的@符号,注掉则输出登录二维码时变慢                           
        # mfriend.send(get_news()[1][5:])                                                     
        mfriend.send(u'moring,早上好呀!')                                                         
        mfriend.send(message)                                                                 
        mfriend.send(u'疫情期间出门要做好防护哟^_^')                                                      
                                                                                              
    except:                                                                                   
        mfriend = bot.friends().search("发光如星")[0]                                             
        mfriend.send(u'failed')                                                               

解析json文件,向json内的好友发送消息

// An highlighted block
var foo = 'bar';
#解析朋友js文件,向多人发送消息                                                                                                 
def parseFriendInform():                                                                                          
    print("解析程序启动")                                                                                               
    file = open("./conf/information.json", encoding="utf-8")                                                      
    confJson = json.load(file)                                                                                    
    for i in confJson:                                                                                            
        j = confJson[i]                                                                                           
        for man in j:                                                                                             
            nickName = j['nickName']                                                                              
            cityPoztion = j['cityPoztion']                                                                        
            callName = j['callName']                                                                              
            # print(nickName,cityPoztion,callName)                                                                
            messige = window.informationInterfaceParmer(callName,cityPoztion)     #天气信息                           
            # messige = window.get_iciba_everyday_chicken_soup(callName)      #关怀信息                               
            send_news(nickName,messige)                                                                           
            break           #中断循环只调用一次                                                                            
                                                                                                                  

利用上面的时间段,定时发送,不要太频繁了,朋友 会烦的[捂脸]