需要导入的库有requsetsrequestlibraryWeChatwxpylibrarytimelibraryimportjson,datetimeimportrequests,itchat,sxtwlfromitchat.contentimport*fromwxpyimportTEXTimporttime注:光理论还不够,这里有一套2020最新Python全栈实战视频教程,点这里,一起免费进步!1.创建日历函数defgetYMD():#获取对应的农历ymc=[u"11",u"12",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"26",u"27",u"28",u"29",u"30",u"31"]甘=["甲","乙","丙","丁","武","冀","庚","心","任","夔"]Zhi=["子","丑","寅","卯","辰","巳","午","未","申","酉","绪","亥""]ShX=["鼠"、"牛"、"虎"、"兔"、"龙"、"蛇"、"马"、"羊"、"猴"、"鸡"、"狗"、"pig"]numCn=["天","一","二","三","四","五","六","七","八","九","十"]农历=sxtwl.Lunar()year=datetime.datetime.now().yearmonth=datetime.datetime.现在().monthrday=datetime.datetime.now().dayday=lunar.getDayBySolar(年,月,rday)d=str(day.y)+"year"+str(day.m)+"month"+str(day.d)+"day"ifday.Lleap:a="run"+ymc[day.Lmc]+"month"+rmc[day.Ldi]+"day"else:a=ymc[day.Lmc]+"month"+rmc[day.Ldi]+"day"b="week"+numCn[day.week]c=Gan[day.Lyear2.tg]+Zhi[day.Lyear2.dz]+"year"+Gan[day.Lmonth2.tg]+Zhi[day.Lmonth2.dz]+"month"+Gan[day.Lday2.tg]+Zhi[day.Lday2.dz]+"Day"txt='今天的日期:'+d+','+b+'\n'+'中国农历:'+a+','+creturntxt#返回当前日期信息2.爬虫爬取iciba的每日语句defget_iciba_everyday_chicken_soup():url='http://open.iciba.com/dsapi/'#iciba的api地址r=requests.get(url)all=json.loads(r.text)Englis=all['content']Chinese=all['note']everyday_soup=Chinese+'\n'+Englis+'\n'returneveryday_soup#返回爱词霸3的日常句子。天气接口函数defget_sentence(api):santence=requests.get(api)returnsantence.json()4.微信图灵机器人智汇代理申请http://www.kaifx.cn/broker/th...defget_response(question):apikey='17216627bdd6495480ec7608fa1f4aeb'url='http://openapi.tuling123.com/...'+apikey+'&info='+questionres=requests.得到(url).json()returnres['text']#微信机器人@itchat.msg_register(TEXT,isFriendChat=True)defauto_reply(msg):print("消息是:%s"%msg['Content'])itchat.send_msg(get_response(msg['Content']),toUserName=msg['FromUserName'])print('auto_reply:%s'%get_response(msg['Content']))5主函数main()if__name__=='__main__':names=input("请输入你要发送的人的微信号:")hours=int(input("请输入发送消息的时间:"))minutes=int(input("请输入发送消息的分钟数:"))number=input("请输入您所在城市的电话号码:")g=getYMD()g1=get_iciba_everyday_chicken_soup()#weather接口为城市编号name='http://t.weather.sojson.com/a...'+number#传参给get_sentenceg2=get_sentence(name)times=g2['cityInfo']forkey,nameintimes.items():city=times['city']parent=times['parent']#字典嵌套字典time1=g2['data']forkey,nameintime1.items():shidu=time1['shidu']pm25=time1['pm25']quality=time1['quality']ganmao=time1['ganmao']time1=g2['data']time2=time1.get('forecast','keydoesnotexist')time2=time2[0]itchat.auto_login(hotReload=True)forkey,nameintime2.items():high=time2['high']low=time2['low']fx=time2['fx']fl=time2['fl']type=time2['type']notice=time2['type']#调用微信机器人users=itchat.search_friends(names)#查找用户userName=users[0]['UserName']whileTrue:t=datetime.datetime.now()t1=t.strftime('%Y-%m-%d%H:%M:%S')hour=t.hourminute=t.minutesecond=t.secondprint('%d:%d:%d'%(hour,minute,second))if小时==小时和分钟==分钟:itchat.send_msg("%s"%g,toUserName=userName)itchat.send_msg('%s'%g1,toUserName=userName)itchat.send_msg('省份:%s\n''城市:%s\n''今日最高气温:%s\n''今日最低气温:%s\n''风向:%s\n''风力:%s\n''湿度:%s\n''PM2.5:%s\n''空气质量:%s\n''敏感性指数:%s\n''天气:%s-%s'%(parent,city,high,low,fx,fl,shidu,pm25,quality,ganmao,type,notice),toUserName=userName)breakelse:time.sleep(5)#延迟5秒continueitchat.run()time.sleep(86400)PS:1.输入城市代码时,找到需要的城市代码通过以下网址https://github.com/baichengzh...2、输入小时时,是24小时格式,早上输入8:00,输入08,不能输入8.3。输入发送的微信时,必须是收件人的微信全名。无法接收备注,无法查询通讯录
