开发者社区 问答 正文

在 email 函数中,如何爬取天气预报和情话并发送邮件?

在 email 函数中,如何爬取天气预报和情话并发送邮件?

展开
收起
圆葱猪肉包 2024-08-21 14:52:48 58 分享 版权
1 条回答
写回答
取消 提交回答
  • 在 email 函数中,首先使用 requests 和 BeautifulSoup 爬取天气预报和情话的网页内容,然后使用正则表达式 re 提取情话。之后,使用 yagmail 发送邮件,邮件内容包含天气预报和情话。示例代码片段如下:
    python
    def email():

    # 爬取天气预报和情话  
    # ...(省略爬取代码)  
    
    # 发送邮件  
    yag = yagmail.SMTP(  
        host='smtp.qq.com', user='xxxxxxx@qq.com',  
        password='xhaztrwpjffpbdhh', smtp_ssl=True  
    )  
    weather_content = [weather.text, "每日情话:", content[ran]]  
    yag.send(  
        to=['xxxxxxxxx@qq.com'],  
        subject='早鸭',  
        contents=weather_content  
    )  
    print("发送完成")
    
    2024-08-21 17:31:51
    赞同 19 展开评论
问答地址: