前言 鼓搗了一陣子的微信機器人今天終於運行成功了,可以選擇在每天的5點20分為你的女朋友發送當日的天氣,每日一句還有日曆,如果你有多個女朋友的話,還可以創建一個列表,之後迴圈遍歷列表為你的多個女朋友發送你的關懷,讓你更好的做好時間管理。 設置好你需要關懷的人的微信名,微信掃碼登錄,剩下的交給程式就可 ...
前言
鼓搗了一陣子的微信機器人今天終於運行成功了,可以選擇在每天的5點20分為你的女朋友發送當日的天氣,每日一句還有日曆,如果你有多個女朋友的話,還可以創建一個列表,之後迴圈遍歷列表為你的多個女朋友發送你的關懷,讓你更好的做好時間管理。
設置好你需要關懷的人的微信名,微信掃碼登錄,剩下的交給程式就可以了~
需要導入的庫有requsets請求庫 微信wxpy庫 time時間庫
Python學習交流Q群:903971231### import json,datetime import requests,itchat,sxtwl from itchat.content import * from wxpy import TEXT import time
1. 創建一個日曆函數
Python學習交流Q群:903971231### 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 # 返回當前的日期信息
2. 爬蟲爬取愛詞霸的每日一句
Python學習交流Q群:903971231 def get_iciba_everyday_chicken_soup(): url = 'http://open.iciba.com/dsapi/' # 愛詞霸的api地址 r = requests.get(url) all = json.loads(r.text) Englis = all['content'] Chinese = all['note'] everyday_soup = Chinese+'\n'+Englis+'\n' return everyday_soup # 返回愛詞霸的每日一句
3. 天氣介面函數
def get_sentence(api): santence = requests.get(api) return santence.json()
4.微信機器人
def get_response(question): apikey = '17216627bdd6495480ec7608fa1f4aeb' url = 'http://openapi.tuling123.com/openapi/api/v2' + apikey + '&info=' + question res = requests.get(url).json() return res['text'] #微信機器人 @itchat.msg_register(TEXT, isFriendChat=True)def auto_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() # 天氣介面的網站 number為城市編號 name = 'http://t.weather.sojson.com/api/weather/city/'+ number # 向get_sentence 傳入參數 g2 = get_sentence(name) times = g2['cityInfo'] for key, name in times.items(): city = times['city'] parent = times['parent'] # 字典嵌套字典 time1 = g2['data'] for key, name in time1.items(): shidu = time1['shidu'] pm25 = time1['pm25'] quality = time1['quality'] ganmao = time1['ganmao'] time1 = g2['data'] time2 = time1.get('forecast', '不存在該鍵') time2 = time2[0] itchat.auto_login(hotReload=True) for key, name in time2.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'] while True: t = datetime.datetime.now() t1=t.strftime('%Y-%m-%d %H:%M:%S') hour = t.hour minute = t.minute second = t.second print('%d:%d:%d' % (hour,minute,second)) if hour == hours and minute == minutes: 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) break else: time.sleep(5) # 延遲5秒 continue itchat.run() time.sleep(86400)
鼓搗了一陣子的微信機器人今天終於運行成功了,可以選擇在每天的5點20分為你的女朋友發送當日的天氣,每日一句還有日曆,如果你有多個女朋友的話,還可以創建一個列表,之後迴圈遍歷列表為你的多個女朋友發送你的關懷,讓你更好的做好時間管理。
設置好你需要關懷的人的微信名,微信掃碼登錄,剩下的交給程式就可以了~
需要導入的庫有requsets請求庫 微信wxpy庫 time時間庫
import json,datetime import requests,itchat,sxtwl from itchat.content import * from wxpy import TEXT import time
1. 創建一個日曆函數
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 # 返回當前的日期信息
2. 爬蟲爬取愛詞霸的每日一句
def get_iciba_everyday_chicken_soup(): url = 'http://open.iciba.com/dsapi/' # 愛詞霸的api地址 r = requests.get(url) all = json.loads(r.text) Englis = all['content'] Chinese = all['note'] everyday_soup = Chinese+'\n'+Englis+'\n' return everyday_soup # 返回愛詞霸的每日一句
3. 天氣介面函數
def get_sentence(api): santence = requests.get(api) return santence.json()
4.微信機器人
def get_response(question): apikey = '17216627bdd6495480ec7608fa1f4aeb' url = 'http://openapi.tuling123.com/openapi/api/v2' + apikey + '&info=' + question res = requests.get(url).json() return res['text'] #微信機器人@itchat.msg_register(TEXT, isFriendChat=True)def auto_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() # 天氣介面的網站 number為城市編號 name = 'http://t.weather.sojson.com/api/weather/city/'+ number # 向get_sentence 傳入參數 g2 = get_sentence(name) times = g2['cityInfo'] for key, name in times.items(): city = times['city'] parent = times['parent'] # 字典嵌套字典 time1 = g2['data'] for key, name in time1.items(): shidu = time1['shidu'] pm25 = time1['