代码利用Python每天自动发新闻到邮箱

Posted Maynor996

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码利用Python每天自动发新闻到邮箱相关的知识,希望对你有一定的参考价值。

前言

偶然间想到每天推送新闻给自己,在网上搜了下果然有轮子,不过代码跑不起来,于是自己改了下
https://blog.csdn.net/qq_42374697/article/details/122144486

# -*- coding: utf-8 -*-
"""
Created on Fri Dec 24 15:21:51 2021

@author: ABC
"""

# smtplib 用于邮件的发信动作
import smtplib
from email.mime.text import MIMEText
# email 用于构建邮件内容
from email.header import Header
# 用于构建邮件头
import json
import hashlib
import base64
import hmac
import os
import time
import requests
from urllib.parse import quote_plus
from lxml import etree
import urllib.request  # 提取网址数据
import gzip


def get_weather_data():
    city_name = '重庆'
    url1 = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
    weather_data = urllib.request.urlopen(url1)
    weather_data = weather_data.read()
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    weather_dict = json.loads(weather_data)
    return weather_dict


def show_weather(weather_data):
    # 将形参数据值即(return weather_dict)赋值给这里的weather_dict变量
    import requests
    from lxml import etree
    base_url = 'https://www.tianqi.com/chongqing/'
    headers = 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko)'
                             + 'Chrome/62.0.3202.94 Safari/537.36'
    res = requests.get(base_url, headers=headers)
    html = res.text
    html = etree.HTML(html)
    riqi = html.xpath('//dl[@class="weather_info"]/dd[@class="week"]/text()')[0].replace(' ', ' ')
    tianqi = html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/span/b/text()')[0]
    now = ''.join(html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/p[@class="now"]//text()'))
    wendu = html.xpath('//dl[@class="weather_info"]/dd[@class="weather"]/span/text()')[0]
    shidu = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[1]/text()')[0]
    shidu1 = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[2]/text()')[0]
    shidu2 = html.xpath('//dl[@class="weather_info"]/dd[@class="shidu"]/b[3]/text()')[0]
    kongqi = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/h5/text()')[0]
    pM = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/h6/text()')[0]
    richu = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/span/text()')[0]
    richu2 = html.xpath('//dl[@class="weather_info"]/dd[@class="kongqi"]/span/text()')[1]
    yu = html.xpath('//div[@class="weather_life300"]/ul/li[1]/a/p/text()')[0]
    weather_dict = weather_data
    if weather_dict.get('desc') == 'invilad-citykey':
        print('你输入的城市名有误,或者天气中心未收录你所在城市')
    elif weather_dict.get('desc') == 'OK':
        forecast = weather_dict.get('data').get('forecast')

        if '雨' in yu:
            tishi = '今日可能下雨,记得带伞哦!' + weather_dict.get('data').get('ganmao')
        else:
            tishi = +weather_dict.get('data').get('ganmao')

        b = '------重庆天气查询------\\n' + \\
            riqi + '\\n' + '天气:' + tianqi + '\\n' + '当前气温:' + now + '\\n' + '温度:' + wendu + '\\n' + shidu + '\\n' + shidu1 + '\\n' + shidu2 + '\\n' + kongqi + '\\n' + \\
            pM + '\\n' + richu + '\\n' + richu2 + '\\n' \\
                                                '-----------------------' + '\\n' \\
                                                                            '明日天气:', forecast[1].get('type') + '\\n' \\
                                                                                                               '最高气温:', \\
            forecast[1].get('high').replace('高温 ', '') + '\\n' \\
                                                         '最低气温:', forecast[1].get('low').replace('低温 ', '') + '\\n' \\
                                                                                                              '温馨提示:', tishi
        return b

def main_handler(arg1,agr2):
    pass

def jianbao():
    base_url = 'https://www.163.com/dy/media/T1603594732083.html'
    headers = 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                             + 'Chrome/62.0.3202.94 Safari/537.36'
    # 获取新闻列表页
    res = requests.get(base_url, headers=headers)
    html = res.text
    html = etree.HTML(html)
    # 获取第一条新闻
    today_url = html.xpath("//ul[@class='list_box cur']/li/a/@href")[0]
    # 获取详情页数据
    resp_1 = requests.get(today_url, headers=headers).text
    html_1 = etree.HTML(resp_1)

    # 获取新闻详情
    news_list = html_1.xpath('//div[@class="post_body"]/p[2]//text()')[1:]
    a = '\\n'.join(news_list)
    a = a.replace('365资讯简报,每天精选15条热点新闻简报1条微语,', '')

    return a

if __name__ == "__main__":
    # 发信方的信息:发信邮箱,QQ 邮箱授权码
    from_addr = 'xx@qq.com'
    password = 'xx'
    # 收信方邮箱
    to_addr = 'xx@qq.com'
    # 发信服务器
    smtp_server = 'smtp.qq.com'
    # 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码

    # b = show_weather(get_weather_data())
    # b = '下午好,xx\\n' + "".join(b)
    a = jianbao()
    c = a+ '\\n' + '\\n' + '------------------------------------\\n'
    msg = MIMEText(eval('c'), 'plain', 'utf-8')
    # 邮件头信息
    msg['From'] = Header(from_addr)
    msg['To'] = Header(to_addr)
    msg['Subject'] = Header('Never say die.下午好,Maynor')
    # 开启发信服务,这里使用的是加密传输
    server = smtplib.SMTP_SSL(smtp_server)
    server.connect(smtp_server, 465)
    # 登录发信邮箱
    server.login(from_addr, password)
    # 发送邮件
    server.sendmail(from_addr, to_addr, msg.as_string())
    # 关闭服务器
    server.quit()

以上是关于代码利用Python每天自动发新闻到邮箱的主要内容,如果未能解决你的问题,请参考以下文章

代码利用Python每天自动发新闻到邮箱

Python 定时发送每日天气和每日简报至邮件或钉钉(代码部署在云服务器)

Python 定时发送每日天气和每日简报至邮件或钉钉(代码部署在云服务器)

Python 定时发送每日天气和每日简报至邮件或钉钉(代码部署在云服务器)

利用Python自动发送邮件

使用Python,SMTP发邮件到qq邮箱(文本/超链接/图片/表格/附件表格)