python自动回复邮件 怎么用python做自动回复机器人( 二 )


重新分析网页XHR即可获取真正的访问连接
https://v2.jinrishici.com/one.json?client=browser-sdk/1.2&X-User-Token=xxxxxx,Token见下图:

python自动回复邮件 怎么用python做自动回复机器人

文章插图
分析好原因后代码反而更加简单了:
import requestsurl = 'https://v2.jinrishici.com/one.json?client=browser-sdk/1.2&X-User-Token=xxxxxx'response = requests.get(url)print(response.json()['data']['content'])
python自动回复邮件 怎么用python做自动回复机器人

文章插图
返回的诗句直接就可以作为函数结果返回,因此代码又可以写成:
import requestsdef get_verse():url = 'https://v2.jinrishici.com/one.json?client=browser-sdk/1.2&X-User-Token=xxxxxx'response = requests.get(url)return f'您要的每日诗句为:{response.json()["data"]["content"]}'
获取天气可以使用官方提供的 API 了,以广州为例:
import requestsurl = 'http://wthrcdn.etouch /weather_mini?city=广州'response = requests.get(url)print(response.json())
python自动回复邮件 怎么用python做自动回复机器人

文章插图
根据返回的 json 数据很容易获取今日的天气情况和最高最低气温,组合成函数效果如下:
def get_weather(city):url = f'http://wthrcdn.etouch /weather_mini?city={city}'response = requests.get(url).json()results = response['data']['forecast'][0]return f'{city}今天的天气情况为{results["type"]},{results["high"][:-1]}度,{results["low"][:-1]}度'
至此,代码部分就写完了 。我们的自动回复机器人也就拥有了两个简单的功能,当然你可以结合自己的需求实现有意思的功能!最后附上完整代码供大家学习与交流
import keyringimport yagmailfrom imbox import Imboximport requestsimport timepassword = keyring.get_password('88mail', 'test88.com')def get_verse():url = 'https://v2.jinrishici.com/one.json?client=browser-sdk/1.2&X-User-Token=xxxxxx'response = requests.get(url)return f'您要的每日诗句为:{response.json()["data"]["content"]}'def get_weather(city):url = f'http://wthrcdn.etouch /weather_mini?city={city}'response = requests.get(url).json()results = response['data']['forecast'][0]return f'{city}今天的天气情况为{results["type"]},{results["high"][:-1]}度,{results["low"][:-1]}度'def send_mail(email, results):mail = yagmail.SMTP(user='test88.com', password=password, host='smtp.88.com')contents = [results]mail.send(email, '【自动回复】您要的信息见正文', contents)def main():with Imbox('imap.88.com', 'test88.com', password, ssl=True) as imbox:unread_inbox_messages = imbox.messages(unread=True)# 获取未读邮件for uid, message in unread_inbox_messages:title = message.subjectemail = message.sent_from[0]['email']results = ''if title == '来句诗':results = get_verse()if title[-2:] == '天气':results = get_weather(title[:-2])if results:send_mail(email, results)imbox.mark_seen(uid)while True:main()time.sleep(600)
【python自动回复邮件 怎么用python做自动回复机器人】Tags:


推荐阅读