python-定时给指定微信女朋友发送天气预报和每日一句自动聊天抢红包抢票1.2019年05月04日16:46:41Harrison.H阅读数460
2.wxpy:用Python玩微信
3.Python定时自动给微信好友发送天气预报
#-*-coding:utf-8-*-from__future__importunicode_literalsfromwxpyimport*importrequestsfromdatetimeimportdatetimeimporttimefromapscheduler.schedulers.blockingimportBlockingScheduler#定时框架fromurllib.requestimporturlopenfrombs4importBeautifulSoupbotBot(cache_pathTrue)#登陆微信tulingTuling(api_key'4a0488cdce684468b95591a641f0971d')#机器人api#单个好友#friendbot.friends().search('harrison')[0]#好友的微信昵称,或者你存取的备注#locationfriend.city#print(friend)#print(friend.city)#好友列表friendlist[ensure_one(bot.search(remark_name'father')),bot.friends().search(remark_name'd')[0],bot.friends().search(remark_name'申')[:],bot.friends().search(remark_name'harrison')[0]]print(friendlist)defget_weather(location):#准备url地址,得出location的结果path'http://api.map.baidu.com/telematics/v3/weather?location%soutputjsonakTueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQcallback?'urlpath%locationresponserequests.get(url)resultresponse.json()str1'你的城市:%s'%location#如果城市错误就按照成都的结果ifresult['error']!0:str1'你的地区%s获取失败,请修改资料。默认参数:深圳'%locationlocation'深圳'urlpath%locationresponserequests.get(url)resultresponse.json()str0('这是明天的天气预报!来自贴心的Harrison')resultsresult['results']#取出数据字典data1results[0]#取出pm2.5值pm25data1['pm25']str2'PM2.5:%s'%pm25#将字符串转换为整数否则无法比较大小pm25int(pm25)ifpm25'':pm250#通过pm2.5的值大小判断污染指数if0pm2535:pollution'优'elif35pm2575:pollution'良'elif75pm25115:pollution'轻度污染'elif115pm25150:pollution'中度污染'elif150pm25250:pollution'重度污染'elifpm25250:pollution'严重污染'str3'空气指数:%s'%pollutionresult1results[0]weather_dataresult1['weather_data']dataweather_data[1]datetimedata['date']temperaturedata['temperature']str4'明天温度:%s%s'%(datetime,temperature)winddata['wind']str5'风向:%s'%windweatherdata['weather']str6'天气:%s'%weather#str7'温度:%s'%data['temperature']messagedata1['index']str8'穿衣:%s'%message[0]['des']#str9'我很贴心:%s'%message[2]['des']#str10'运动:%s'%message[3]['des']str11'紫外线:%s'%message[4]['des']#str12"请注意身体~"strstr0+str1+str2+str3+str4+str5+str6+str8+str11returnstrdefget_iciba():url"http://open.iciba.com/dsapi/"rrequests.get(url)contentr.json()['content']noter.json()['note']str'每日一句:'+content+''+note+''returnstr#发送函数defsend_message():foriinrange(len(friendlist)):friendfriendlist[i]locationfriend.cityprint(i+1,'/%s'%len(friendlist),'姓名:%s'%friend,'地区:%s'%location)textget_weather(friend.city)+get_iciba()+'好梦~'friend.send(text)#发送成功通知我bot.file_helper.send(friend)bot.file_helper.send('发送完毕')return#执行程序时直接发送#send_message()#定时器print('start')schedBlockingScheduler()sched.add_job(send_message,'cron',day_of_week'0-6',hour23,minute00)#设定发送的时间sched.start()4.自动聊天抢红包抢票
#-*-coding:utf-8-*-'''这一次对上一个demo进行了扩充主要是添加了tuling机器人的自动回复以及让python进程保持且可以调试主要还是归功于wxpy的embed()函数。。。tulingKey需要自己申请很简单,上网站注册一下账号就ok了下面给出图灵网址:http://www.tuling123.com/然后就是wxpy的灵活使用,详情可以参照官网的API文档网址:http://wxpy.readthedocs.io/zh/latest/index.html然后接下来就是我写的一个小例子主要功能有1、单人聊天消息反馈(备注在代码里改,懒得写交互了,反正大家都懂得)2、单人聊天消息发送(根据输入的备注,这里写了简单的交互,没写exception处理)3、图灵自动聊天(可以通过关闭注册函数来关闭或者开启)注意此代码不可直接复制粘贴:1、需要自己申请tulingkey赋值2、需要指定进程保持时候和谁通信当然也可以自己做修改自由发挥!'''from__future__importunicode_literalsfromwxpyimport*importrequestsfromthreadingimportTimerfromtkinterimport*tulingKey'xxxxx'itchatBot(console_qr2,cache_path"botoo.pkl")frienditchat.friends().search(nameu'xxxx')tulingTuling(api_keytulingKey)defgetNews():url"http://open.iciba.com/dsapi/"rrequests.get(url)contentr.json()['content']noter.json()['note']returncontent,note#tuling自动回复的消息注册defreply(msg):returntuling.do_reply(msg)#接受到消息后的打印@itchat.register(msg_typesTEXT)defprintMsg(msg):print(msg)print('tuling自动回复消息---'+reply(msg))#发送每日一句消息函数defsendNews():try:#这里是备注printcontentgetNews()message1str(content[0])message2str(content[1])print('--------------今日内容:-------------'+message1+''+message2+"---------------------------------")#message3"进行最后一项测试embed()"print('----当前friend对象为:'+str(friend))forindex,iteminenumerate(friend):rightMarkTruewhilerightMark:print("---------------将要发送给"+str(item)+"ing,index"+str(index)+'是否发送?')orderinput("-----------------请输入指令(1是2否3退出当前操作循环):")orderint(order)if(order1):rightMarkFalseitem.send(message1)item.send(message2)print("----------消息发送成功----------------")elif(order2):rightMarkFalseprint("----------消息发送操作被忽略-----------")continueelif(order3):rightMarkFalseprint("----------消息发送程序退出------------")breakelse:print("-------指令错误,请重新输入!")except:errorMessage"BugBugBug!"forindex,iteminenumerate(friend):item.send(errorMessage)defsendMessage():nameinput("请输入好友备注:")frienditchat.friends().search(namename)if(len(friend)0):print("找寻到备注为"+name+"的好友"+str(friend)+"如果存在多个该备注好友,默认选择第一个人发送")sendMessageByFriend(friend[0])else:print("未找到备注为:"+name+"的好友!")defsendMessageByFriend(friend):rightMarkTruewhilerightMark:messinput("------请输入您要发的信息:")orderinput("----------即将发送的信息为:"+mess+"请选择是否发送0是1否--------")orderint(order)if(order0):friend.send(mess)print("----------消息已成功发送(注意此处无catch)-------")rightMarkFalseelif(order1):print("----------消息未发送--------")rightMarkFalseelse:print("---------指令错误,请重新输入!")if__name__"__main__":#sendNews()embed()5.每日一句
#-*-coding:utf-8-*-'''这是一个用来测试微信自动发送消息的demo参考了一篇博客博客地址:http://www.cnblogs.com/botoo/p/8622379.html恩,主要就是用到了一个微信库--wxpy安装很简单pipinstallwxpy下面就开始吧主要就两个函数1、getNews();用以获取信息2、sendNews();用以发送信息我这里发送消息用的是for循环本意是群发,但是!但是!但是!程序发的太快会被微信禁掉,大概40个人左右就会被禁,以后可以试试sleep一下。另外vscode中自定义python编译器:Ctrl+shift+p,选择python:SelectInterpreter'''from__future__importunicode_literalsfromwxpyimport*importrequestsfromthreadingimportTimeritchatBot(console_qr2,cache_path"botoo.pkl")defgetNews():url"http://open.iciba.com/dsapi/"rrequests.get(url)contentr.json()['content']noter.json()['note']returncontent,notedefsendNews():try:#这里是备注frienditchat.friends().search(nameu'xxx')contentgetNews()print(content)message1str(content[0])message2str(content[1])message3"xxx"print(friend)forindex,iteminenumerate(friend):print("发送给"+str(item)+"ing,index"+str(index))item.send(message1)item.send(message2)item.send(message3)tTimer(86400,sendNews)t.start()except:errorMessage"xxx"forindex,iteminenumerate(friend):item.send(errorMessage)if__name__"__main__":sendNews()
python-定时给指定微信女朋友发送天气预报和每日一句自动聊天抢红包抢票-每天一条微信天气预报-微信小程序开发天气预报
浏览量:1166
时间:
来源:HOLD ON!
版权声明
即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

最新资讯
-
抖音再现本地生活服务,咫尺同城圈商业变现新通道
短视频成为本地生活探店网红营销变现引流的新阵地,每一位网红都渴望在短视频内“一夜爆红”。即速应用团队对多商家小程序进行升级,打造了咫尺同城圈:“同城探店营销助手”,不仅完善商家营销技巧,还助力探店网红玩转本地生活服务。 -
抖音再现本地生活服务,咫尺同城圈商业变现新通道
短视频成为本地生活探店网红营销变现引流的新阵地,每一位网红都渴望在短视频内“一夜爆红”。即速应用团队对多商家小程序进行升级,打造了咫尺同城圈:“同城探店营销助手”,不仅完善商家营销技巧,还助力探店网红玩转本地生活服务。 -
阿坝小程序代理
阿坝藏族羌族小程序代理公司有哪些?阿坝藏族羌族小程序代理平台哪个好?阿坝藏族羌族小程序代理商怎么收费,代理政策如何?下面就让即速应用产品经理jisuapp.cn来告诉你吧!