给女朋友做个聊天机器人,这样就能安心写代码了
這是第 404 次女友因?yàn)槲覜]有及時(shí)回微信而和我生氣了
惹禍的是我,受傷的確是錢包……
但是,我并不后悔
那一局王者榮耀,因?yàn)闆]有回微信,我完成了五殺 carry 全場(chǎng)
那一局刺激戰(zhàn)場(chǎng),因?yàn)闆]有回微信,我絕地反殺成功吃雞
那一場(chǎng)球賽,因?yàn)闆]有回微信,我看到了極限投籃壓哨三分
最重要的是,因?yàn)闆]有回微信,我終于可以安心寫代碼
兩耳不聞窗外事,一心只想寫代碼,這才是一名程序員的內(nèi)心獨(dú)白
作為程序員中的佼佼者,深知有個(gè)女友不容易,于是,為了更及時(shí)的陪女友聊天回復(fù)微信,活學(xué)活用的程序員用 Python + itchat 寫一個(gè)爬蟲腳本每天定時(shí)給女友發(fā)給微信高階土味情話。
再也不怕沒能及時(shí)回復(fù)女友的微信了,女友甚至喜歡上了那個(gè)陪她聊天的機(jī)器人
核心代碼
1. 定時(shí)任務(wù)
每天 9:30 給女朋友們開始給女朋友發(fā)送內(nèi)容。
# 定時(shí)任務(wù) scheduler = BlockingScheduler() # 每天9:30給女朋友發(fā)送每日一句 # scheduler.add_job(start_today_info, 'cron', hour=9, minute=30) scheduler.start()start_today_info 是方法處理類。
2. 獲取每日一句
數(shù)據(jù)來源 1: ONE●一個(gè)
def get_dictum_info(self):'''獲取格言信息(從『一個(gè)。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 -個(gè)』 中的每日一句every_msg = soup_texts.find_all('div', class_='fp-one-cita')[0].find('a').textreturn every_msg數(shù)據(jù)來源 2: 金山詞霸 ● 每日一句
有英文和中文翻譯,例如:
When you finally get your own happiness, you will understand the previous sadness is a kind of treasure, which makes you better to hold and cherish the people you love.等你獲得真正屬于你的幸福之后,你就會(huì)明白一起的傷痛其實(shí)是一種財(cái)富,它讓你學(xué)會(huì)更好地去把握和珍惜你愛的人。代碼實(shí)現(xiàn) :
def get_ciba_info(self):'''從詞霸中獲取每日一句,帶英文。:return:'''resp = requests.get('http://open.iciba.com/dsapi')if resp.status_code == 200 and self.isJson(resp):conentJson = resp.json()content = conentJson.get('content')note = conentJson.get('note')# print(f"{content} {note}")return f"{content} {note} "else:print("沒有獲取到數(shù)據(jù)")return None數(shù)據(jù)來源 3: 土味情話(感謝 tomatoF、QSCTech-Sange)
def get_lovelive_info(self):'''從土味情話中獲取每日一句。'''resp = requests.get("https://api.lovelive.tools/api/SweetNothings")if resp.status_code == 200:return resp.text + " "else:print('每日一句獲取失敗')return None3. 獲取今日天氣
天氣數(shù)據(jù)來源:SOJSON
def get_weather_info(self, city_code=''):weather_url = f'http://t.weather.sojson.com/api/weather/city/{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]city_code 城市對(duì)應(yīng) id。 http://cdn.sojson.com/_city.json
4. 登錄微信并發(fā)送內(nèi)容
itchat.auto_login() itchat.send(today_msg, toUserName=name_uuid)就是這么簡(jiǎn)單,女友再也不會(huì)打擾我寫代碼、玩游戲了!
不會(huì) Python 怎么辦?
還有個(gè) Node+ wechaty 的,小編能幫你的只能到這里了
Python + itchat GitHub 地址:https://github.com/sfyc23/EverydayWechat
Node+ wechaty GitHub 地址:https://github.com/gengchen528/wechatBot
萬事俱備,只缺一個(gè)女朋友!
請(qǐng)問各位有女朋友的程序員,女朋友是哪里來的?
充話費(fèi)還是充網(wǎng)費(fèi)?送女朋友嗎?
小編也想要一個(gè),家里人催的緊,在線等,挺急的!
關(guān)注微信公眾號(hào)【禿頭哥編程】,領(lǐng)取編程大禮包。
總結(jié)
以上是生活随笔為你收集整理的给女朋友做个聊天机器人,这样就能安心写代码了的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: w10投影全屏设置_win10如何让投影
- 下一篇: 最全电商分类信息(02)