Python利用itchat模塊定時(shí)給朋友發(fā)送微信信息
功能
定時(shí)給女朋友發(fā)送每日天氣、提醒、每日一句。
數(shù)據(jù)來(lái)源
每日一句和上面的大佬一樣也是來(lái)自O(shè)NE·一個(gè)
天氣信息來(lái)自SOJSON
實(shí)現(xiàn)效果


代碼說(shuō)明
目錄結(jié)構(gòu)

city_dict.py :城市對(duì)應(yīng)編碼字典
config.yaml :設(shè)置定時(shí)時(shí)間,女友微信名稱等參數(shù)
GFWeather.py:核心代碼
requirements.txt:需要安裝的庫(kù)
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"每天定時(shí)發(fā)送時(shí)間:{alarm_timed}\n女友所在城市名稱:{city_name}\n女朋友的微信昵稱:{wechat_name}\n在一起的第一天日期:{start_date}"
print(u"*" * 50)
print(init_msg)
# 根據(jù)城市名稱獲取城市編號(hào),用于查詢天氣。查看支持的城市為:http://cdn.sojson.com/_city.json
city_code = city_dict.city_dict.get(city_name)
if not city_code:
print('您輸出城市無(wú)法收取到天氣信息')
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,如果掉線了則自動(dòng)登錄。
:return: True ,還在線,F(xiàn)alse 不在線了
'''
def online():
'''
通過(guò)獲取好友信息,判斷用戶是否還在線
: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):
# 自動(dòng)登錄
if not self.is_online(auto_login=True):
return
# 定時(shí)任務(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('昵稱錯(cuò)誤')
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):
'''
獲取格言信息(從『一個(gè)。one』獲取信息 http://wufazhuce.com/)
:return: str 一句格言或者短語(yǔ)
'''
print('獲取格言信息..')
user_url = 'http://wufazhuce.com/'
resp = requests.get(user_url, headers=self.headers)
soup_texts = BeautifulSoup(resp.text, 'lxml')
# 『one -個(gè)』 中的每日一句
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'寶貝這是我們?cè)谝黄鸬牡?{day_delta} 天'
today_msg = f'{today_time}\n{delta_msg}。\n{notice}\n{temperature}\n{wind}\n{aqi}\n{dictum_msg}\n來(lái)自最愛(ài)你的我。'
return today_msg項(xiàng)目運(yùn)行
安裝依賴
使用 pip install -r requirements.txt 安裝所有依賴
參數(shù)配置
config.yaml
#每天定時(shí)發(fā)送的時(shí)間點(diǎn),如:8:30 alarm_timed: '9:30' # 女友所在城市名稱 city_name: '桂林' # 你女朋友的微信名稱 wechat_name: '古典' # 從那天開(kāi)始勾搭的 start_date: '2017-11-11'
到此這篇關(guān)于Python利用itchat模塊定時(shí)給朋友發(fā)送微信信息的文章就介紹到這了,更多相關(guān)Python itchat定時(shí)發(fā)送微信信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pyqt5與matplotlib的完美結(jié)合實(shí)例
今天小編就為大家分享一篇pyqt5與matplotlib的完美結(jié)合實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
python中@contextmanager實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于python中@contextmanager實(shí)例用法,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。2021-02-02
python 使用elasticsearch 實(shí)現(xiàn)翻頁(yè)的三種方式
這篇文章主要介紹了python 使用elasticsearch 實(shí)現(xiàn)翻頁(yè)的三種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
python中使用pyhook實(shí)現(xiàn)鍵盤監(jiān)控的例子
這篇文章主要介紹了python中使用pyhook實(shí)現(xiàn)鍵盤監(jiān)控的例子,包含pyhook的下載地址和手冊(cè)地址及一個(gè)Windows下的監(jiān)控實(shí)例,需要的朋友可以參考下2014-07-07
python+requests實(shí)現(xiàn)接口測(cè)試的完整步驟
這篇文章主要給大家介紹了關(guān)于python+requests實(shí)現(xiàn)接口測(cè)試的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
python將多個(gè)py文件和其他文件打包為exe可執(zhí)行文件
這篇文章主要介紹了python將多個(gè)py文件和其他文件打包為exe可執(zhí)行文件,通過(guò)準(zhǔn)備要打包的工程文件展開(kāi)詳情,需要的小伙伴可以參考一下2022-05-05
python使用datetime.utcnow()問(wèn)題解析
這篇文章主要介紹了python使用datetime.utcnow()問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
java中的控制結(jié)構(gòu)(if,循環(huán))詳解
這篇文章簡(jiǎn)單地介紹了java中的控制結(jié)構(gòu)(if,循環(huán))文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面我們來(lái)學(xué)習(xí)下吧2019-06-06
詳解Python的Django框架中的模版相關(guān)知識(shí)
這篇文章主要介紹了Python的Django框架中的模版相關(guān)知識(shí),模版的存在大大簡(jiǎn)化了創(chuàng)作頁(yè)面時(shí)HTML的相關(guān)工作,需要的朋友可以參考下2015-07-07
Python利用pandas和matplotlib實(shí)現(xiàn)繪制圓環(huán)圖
在可視化的過(guò)程中,圓環(huán)圖是一種常用的方式,特別適合于展示各類別占比情況,本文將介紹如何使用 Python中的 pandas 和 matplotlib 庫(kù),來(lái)制作一個(gè)店鋪銷量占比的圓環(huán)圖,需要的可以參考下2023-11-11

