Python利用itchat模塊定時給朋友發(fā)送微信信息
功能
定時給女朋友發(fā)送每日天氣、提醒、每日一句。
數(shù)據(jù)來源
每日一句和上面的大佬一樣也是來自O(shè)NE·一個
天氣信息來自SOJSON
實(shí)現(xiàn)效果
代碼說明
目錄結(jié)構(gòu)
city_dict.py :城市對應(yīng)編碼字典
config.yaml :設(shè)置定時時間,女友微信名稱等參數(shù)
GFWeather.py:核心代碼
requirements.txt:需要安裝的庫
run.py:項(xiàng)目運(yùn)行類
核心代碼
GFWeather.py
class gfweather: headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36", } # 女朋友的用戶id bf_wechat_name_uuid = '' def __init__(self): self.city_code, self.start_datetime, self.bf_wechat_name, self.alarm_hour, self.alarm_minute = self.get_init_data() def get_init_data(self): ''' 初始化基礎(chǔ)數(shù)據(jù) :return: ''' with open('config.yaml', 'r', encoding='utf-8') as f: config = yaml.load(f) city_name = config.get('city_name').strip() start_date = config.get('start_date').strip() wechat_name = config.get('wechat_name').strip() alarm_timed = config.get('alarm_timed').strip() init_msg = f"每天定時發(fā)送時間:{alarm_timed}\n女友所在城市名稱:{city_name}\n女朋友的微信昵稱:{wechat_name}\n在一起的第一天日期:{start_date}" print(u"*" * 50) print(init_msg) # 根據(jù)城市名稱獲取城市編號,用于查詢天氣。查看支持的城市為:http://cdn.sojson.com/_city.json city_code = city_dict.city_dict.get(city_name) if not city_code: print('您輸出城市無法收取到天氣信息') start_datetime = datetime.strptime(start_date, "%Y-%m-%d") hour, minute = [int(x) for x in alarm_timed.split(':')] # print(hour, minute) return city_code, start_datetime, wechat_name, hour, minute def is_online(self, auto_login=False): ''' 判斷是否還在線, :param auto_login:True,如果掉線了則自動登錄。 :return: True ,還在線,F(xiàn)alse 不在線了 ''' def online(): ''' 通過獲取好友信息,判斷用戶是否還在線 :return: True ,還在線,F(xiàn)alse 不在線了 ''' try: if itchat.search_friends(): return True except: return False return True if online(): return True # 僅僅判斷是否在線 if not auto_login: return online() # 登陸,嘗試 5 次 for _ in range(5): # 命令行顯示登錄二維碼 # itchat.auto_login(enableCmdQR=True) itchat.auto_login() if online(): print('登錄成功') return True else: return False def run(self): # 自動登錄 if not self.is_online(auto_login=True): return # 定時任務(wù) scheduler = BlockingScheduler() # 每天9:30左右給女朋友發(fā)送每日一句 scheduler.add_job(self.start_today_info, 'cron', hour=self.alarm_hour, minute=self.alarm_minute) scheduler.start() def start_today_info(self): print("*" * 50) print('獲取相關(guān)信息...') dictum_msg = self.get_dictum_info() today_msg = self.get_weather_info(dictum_msg) print(f'要發(fā)送的內(nèi)容:\n{today_msg}') if self.is_online(auto_login=True): # 獲取好友username if not self.bf_wechat_name_uuid: friends = itchat.search_friends(name=self.bf_wechat_name) if not friends: print('昵稱錯誤') return self.bf_wechat_name_uuid = friends[0].get('UserName') itchat.send(today_msg, toUserName=self.bf_wechat_name_uuid) print('發(fā)送成功..\n') def get_dictum_info(self): ''' 獲取格言信息(從『一個。one』獲取信息 http://wufazhuce.com/) :return: str 一句格言或者短語 ''' print('獲取格言信息..') user_url = 'http://wufazhuce.com/' resp = requests.get(user_url, headers=self.headers) soup_texts = BeautifulSoup(resp.text, 'lxml') # 『one -個』 中的每日一句 every_msg = soup_texts.find_all('div', class_='fp-one-cita')[0].find('a').text return every_msg def get_weather_info(self, dictum_msg=''): ''' 獲取天氣信息。網(wǎng)址:https://www.sojson.com/blog/305.html :param dictum_msg: 發(fā)送給朋友的信息 :return: ''' print('獲取天氣信息..') weather_url = f'http://t.weather.sojson.com/api/weather/city/{self.city_code}' resp = requests.get(url=weather_url) if resp.status_code == 200 and resp.json().get('status') == 200: weatherJson = resp.json() # 今日天氣 today_weather = weatherJson.get('data').get('forecast')[1] locale.setlocale(locale.LC_CTYPE, 'chinese') today_time = datetime.now().strftime('"%Y年%m月%d日 %H:%M:%S"') # 今日天氣注意事項(xiàng) notice = today_weather.get('notice') # 溫度 high = today_weather.get('high') high_c = high[high.find(' ') + 1:] low = today_weather.get('low') low_c = low[low.find(' ') + 1:] temperature = f"溫度 : {low_c}/{high_c}" # 風(fēng) fx = today_weather.get('fx') fl = today_weather.get('fl') wind = f"{fx} : {fl}" # 空氣指數(shù) aqi = today_weather.get('aqi') aqi = f"空氣 : {aqi}" day_delta = (datetime.now() - self.start_datetime).days delta_msg = f'寶貝這是我們在一起的第 {day_delta} 天' today_msg = f'{today_time}\n{delta_msg}。\n{notice}\n{temperature}\n{wind}\n{aqi}\n{dictum_msg}\n來自最愛你的我。' return today_msg
項(xiàng)目運(yùn)行
安裝依賴
使用 pip install -r requirements.txt 安裝所有依賴
參數(shù)配置
config.yaml
#每天定時發(fā)送的時間點(diǎn),如:8:30 alarm_timed: '9:30' # 女友所在城市名稱 city_name: '桂林' # 你女朋友的微信名稱 wechat_name: '古典' # 從那天開始勾搭的 start_date: '2017-11-11'
到此這篇關(guān)于Python利用itchat模塊定時給朋友發(fā)送微信信息的文章就介紹到這了,更多相關(guān)Python itchat定時發(fā)送微信信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pyqt5與matplotlib的完美結(jié)合實(shí)例
今天小編就為大家分享一篇pyqt5與matplotlib的完美結(jié)合實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python中@contextmanager實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于python中@contextmanager實(shí)例用法,對此有興趣的朋友們可以學(xué)習(xí)下。2021-02-02python 使用elasticsearch 實(shí)現(xiàn)翻頁的三種方式
這篇文章主要介紹了python 使用elasticsearch 實(shí)現(xiàn)翻頁的三種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07python中使用pyhook實(shí)現(xiàn)鍵盤監(jiān)控的例子
這篇文章主要介紹了python中使用pyhook實(shí)現(xiàn)鍵盤監(jiān)控的例子,包含pyhook的下載地址和手冊地址及一個Windows下的監(jiān)控實(shí)例,需要的朋友可以參考下2014-07-07python+requests實(shí)現(xiàn)接口測試的完整步驟
這篇文章主要給大家介紹了關(guān)于python+requests實(shí)現(xiàn)接口測試的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10python將多個py文件和其他文件打包為exe可執(zhí)行文件
這篇文章主要介紹了python將多個py文件和其他文件打包為exe可執(zhí)行文件,通過準(zhǔn)備要打包的工程文件展開詳情,需要的小伙伴可以參考一下2022-05-05java中的控制結(jié)構(gòu)(if,循環(huán))詳解
這篇文章簡單地介紹了java中的控制結(jié)構(gòu)(if,循環(huán))文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,下面我們來學(xué)習(xí)下吧2019-06-06詳解Python的Django框架中的模版相關(guān)知識
這篇文章主要介紹了Python的Django框架中的模版相關(guān)知識,模版的存在大大簡化了創(chuàng)作頁面時HTML的相關(guān)工作,需要的朋友可以參考下2015-07-07Python利用pandas和matplotlib實(shí)現(xiàn)繪制圓環(huán)圖
在可視化的過程中,圓環(huán)圖是一種常用的方式,特別適合于展示各類別占比情況,本文將介紹如何使用 Python中的 pandas 和 matplotlib 庫,來制作一個店鋪銷量占比的圓環(huán)圖,需要的可以參考下2023-11-11