小言_互联网的博客

用python实现给女朋友定时推送

430人阅读  评论(0)

鼓捣了一阵子的微信机器人今天终于运行成功了,可以选择在每天的5点20分为你的女朋友发送当日的天气,每日一句还有日历,如果你有多个女朋友的话,还可以创建一个列表,之后循环遍历列表为你的多个女朋友发送你的关怀,让你更好的做好时间管理
设置好你需要关怀的人的微信名,微信扫码登录,剩下的交给程序就可以了


  
  1. 需要导入的库有requsets请求库 微信wxpy库 time时间库
  2. import json,datetime
  3. import requests,itchat,sxtwl
  4. from itchat.content import *
  5. from wxpy import TEXT
  6. import time

注意:光理论是不够的,在此送大家一套2020最新Python全栈实战视频教程,点击此处 免费获取一起进步哦!

1.创建一个日历函数


  
  1. def getYMD(): #获得对应的农历
  2. ymc = [ u"十一", u"十二", u"正", u"二", u"三", u"四", u"五", u"六", u"七", u"八", u"九", u"十"]
  3. rmc = [ u"初一", u"初二", u"初三", u"初四", u"初五", u"初六", u"初七", u"初八", u"初九", u"初十",
  4. u"十一", u"十二", u"十三", u"十四", u"十五", u"十六", u"十七", u"十八", u"十九",
  5. u"二十", u"廿一", u"廿二", u"廿三", u"廿四", u"廿五", u"廿六", u"廿七", u"廿八", u"廿九", u"三十", u"卅一"]
  6. Gan = [ "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
  7. Zhi = [ "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
  8. ShX = [ "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
  9. numCn = [ "天", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
  10. lunar = sxtwl.Lunar()
  11. year = datetime.datetime.now().year
  12. month = datetime.datetime.now().month
  13. rday = datetime.datetime.now().day
  14. day = lunar.getDayBySolar(year, month, rday)
  15. d = str(day.y) + "年" + str(day.m) + "月" + str(day.d) + "日"
  16. if day.Lleap:
  17. a = "润" + ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日"
  18. else:
  19. a = ymc[day.Lmc] + "月" + rmc[day.Ldi] + "日"
  20. b = "星期" + numCn[day.week]
  21. c = Gan[day.Lyear2.tg] + Zhi[day.Lyear2.dz] + "年" + Gan[day.Lmonth2.tg] + Zhi[day.Lmonth2.dz] + "月" + Gan[
  22. day.Lday2.tg] + Zhi[day.Lday2.dz] + "日"
  23. txt = '今天日期:'+d + ', ' + b + '\n'+ '中华农历: ' + a + ', ' + c
  24. return txt # 返回当前的日期信息

2.爬虫爬取爱词霸的每日一句


  
  1. def get_iciba_everyday_chicken_soup():
  2. url = 'http://open.iciba.com/dsapi/' # 爱词霸的api地址
  3. r = requests.get(url)
  4. all = json.loads(r.text)
  5. Englis = all[ 'content']
  6. Chinese = all[ 'note']
  7. everyday_soup = Chinese+ '\n'+Englis+ '\n'
  8. return everyday_soup # 返回爱词霸的每日一句

3.天气接口函数


  
  1. def get_sentence(api):
  2. santence = requests.get(api)
  3. return santence.json()

4.微信图灵机器人


  
  1. def get_response(question):
  2. apikey = '17216627bdd6495480ec7608fa1f4aeb'
  3. url = 'http://openapi.tuling123.com/openapi/api/v2' + apikey + '&info=' + question
  4. res = requests.get(url).json()
  5. return res[ 'text']
  6. # 微信机器人
  7. @itchat.msg_register(TEXT, isFriendChat=True)
  8. def auto_reply(msg):
  9. print( "消息是:%s" % msg[ 'Content'])
  10. itchat.send_msg(get_response(msg[ 'Content']), toUserName=msg[ 'FromUserName'])
  11. print( 'auto_reply:%s' % get_response(msg[ 'Content']))

5主函数main()


  
  1. if __name__ == '__main__':
  2. names = input( "请输入你要发送人的微信名:")
  3. hours = int(input( "请输入几点发送消息:"))
  4. minutes = int(input( "请输入几分发送消息:"))
  5. number = input( "输入所在城市的编号:")
  6. g = getYMD()
  7. g1 = get_iciba_everyday_chicken_soup()
  8. # 天气接口的网站 number为城市编号
  9. name = 'http://t.weather.sojson.com/api/weather/city/'+ number
  10. # 向get_sentence 传入参数
  11. g2 = get_sentence(name)
  12. times = g2[ 'cityInfo']
  13. for key, name in times.items():
  14. city = times[ 'city']
  15. parent = times[ 'parent']
  16. # 字典嵌套字典
  17. time1 = g2[ 'data']
  18. for key, name in time1.items():
  19. shidu = time1[ 'shidu']
  20. pm25 = time1[ 'pm25']
  21. quality = time1[ 'quality']
  22. ganmao = time1[ 'ganmao']
  23. time1 = g2[ 'data']
  24. time2 = time1.get( 'forecast', '不存在该键')
  25. time2 = time2[ 0]
  26. itchat.auto_login(hotReload= True)
  27. for key, name in time2.items():
  28. high = time2[ 'high']
  29. low = time2[ 'low']
  30. fx = time2[ 'fx']
  31. fl = time2[ 'fl']
  32. type = time2[ 'type']
  33. notice = time2[ 'type']
  34. # 调用微信机器人
  35. users = itchat.search_friends(names) # 找到用户
  36. userName = users[ 0][ 'UserName']
  37. while True:
  38. t = datetime.datetime.now()
  39. t1=t.strftime( '%Y-%m-%d %H:%M:%S')
  40. hour = t.hour
  41. minute = t.minute
  42. second = t.second
  43. print( '%d:%d:%d' % (hour,minute,second))
  44. if hour == hours and minute == minutes:
  45. itchat.send_msg( "%s" % g, toUserName=userName)
  46. itchat.send_msg( '%s' % g1, toUserName=userName)
  47. itchat.send_msg( '所在省份:%s\n'
  48. '所在城市:%s\n'
  49. '今日最高温度:%s\n '
  50. '今日最低温度:%s\n'
  51. '风向:%s\n '
  52. '风力:%s\n'
  53. '湿度:%s \n'
  54. 'PM2.5: %s\n'
  55. '空气质量:%s \n'
  56. '易感指数:%s\n'
  57. '天气:%s - %s '%(parent,city,high,low,fx,fl,shidu,pm25,
  58. quality,ganmao,type,notice), toUserName=userName)
  59. break
  60. else:
  61. time.sleep( 5) # 延迟5秒
  62. continue
  63. itchat.run()
  64. time.sleep( 86400)

PS:

1.输入城市编码时,通过下列网址查找所希望的城市编码

https://github.com/baichengzhou/weather.api/blob/master/src/main/resources/citycode-2019-08-23.json

2.在输入小时数的时候为24小时格式,输入早上8点,要输入08,不能输入8.

3.输入发送的微信时,必须是接收人的微信全名,备注不可以接收,会查询不到通讯录

愿你在我看不到的地方安然无恙,

愿你的冬天永远不缺暖阳。

愿你的明天不再经历雨打风霜,

愿你的未来永远热泪盈眶            --写给前任


转载:https://blog.csdn.net/worrybout/article/details/106657957
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场