热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python发送每日新闻(python+selenium)

本文主要介绍关于python,前端,javascript的知识点,对【Python发送每日新闻】和【python+selenium】有兴趣的朋友可以看下由【A-L-Kun】投稿的技术文章,希望该技术

本文主要介绍关于python,前端,Javascript的知识点,对【Python 发送每日新闻】和【python+selenium】有兴趣的朋友可以看下由【A-L-Kun】投稿的技术文章,希望该技术和经验能帮到你解决你所遇的python,服务器,github相关技术问题。

python+selenium

文章目录 Python 发送每日新闻一、 简介1、 概述2、 环境配置3、 配置文件 二、 前端页面三、 获取数据1、 获取图片2、 获取新闻3、 制作数据 四、 发送邮件

Python 发送每日新闻 一、 简介 1、 概述

关注时事新闻,是新时代青年必须做的,那么,我们如何来快速获取新闻呢?

每天自动从网上找到新闻

自动整理新闻排版成一个html页面,发送到邮箱中

2、 环境配置
requests = "*"  # 用来解析数据
fake-useragent = "*"  # 随机请求头
pyquery = "*"  # 改写前端页面的接口,制作每日快报
3、 配置文件
{
   
  "status": 200,
  "data": [
      {
   "name": "A.L.Kun", "email": "3500515050@qq.com"}
  ],
  "temp": [
  ]
}

data里面存放我们需要发送的对象

status:判断是否获取成功

二、 前端页面

我们使用前端知识,来设计页面,我的代码如下:


    doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="token punctuation">"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>每日简报
      title> <style> /*rest.css*/ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
        margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } * {
        color: black; } input {
        padding: 0; margin: 0; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
        display: block; } body {
        line-height: 1; } ol, ul {
        list-style: none; } blockquote, q {
        quotes: none; } blockquote:before, blockquote:after, q:before, q:after {
        content: ''; content: none; } table {
        border-collapse: collapse; border-spacing: 0; } * {
        text-decoration: none; margin: 0; } 
       style> <style> .main {
         /*设置主窗口的大小,同时居中对齐*/ height: auto; width: 500px; margin: 100px auto; border: #a5a5a5 4px solid; border-radius: 10px; padding-bottom: 10px; } .main > h1 {
         /*设置标题*/ font: normal 500 33px "KaiTi_GB2312"; text-align: center; margin-bottom: 30px; margin-top: 10px; color: #436d7b; } .img_des {
         /*设置图片的描述信息样式*/ font: normal 500 9px "Microsoft YaHei"; text-align: center; color: #b647a6; } img {
         /*设置图片样式*/ padding-left: 2px; border-radius: 10px; margin-bottom: 4px; } .big_line {
         height: 5px; border-radius: 3px; width: 496px; background-color: #48745b; margin: 15px 0 5px 2px; } .small_line {
         height: 3px; border-radius: 1px; width: 496px; background-color: #48745b; margin-left: 2px; } .body {
         overflow: hidden; } .body .left {
         float: left; background-color: #48745b; height: 100px; width: 300px; margin-left: 4px; } .body span {
         margin-top: 10px; border-radius: 4px; } .body .right {
         background-color: #48745b; height: 100px; width: 170px; float: right; margin-right: 4px; } .left h1 {
         color: white; font: normal 500 33px "KaiTi_GB2312"; text-align: center; padding-top: 2px; } .left h2 {
         color: white; font: normal 500 20px "KaiTi_GB2312"; text-align: center; border-top: white 2px solid; margin-top: 10px; padding-top: 6px; } .right h1{
         text-align: center; border-top: #48745b 4px solid; border-left: #48745b 4px solid; border-right: #48745b 4px solid; border-radius: 4px; background-color: white; font: normal 600 18px "KaiTi_GB2312"; color: #48745b; margin-top: 11px; margin-bottom: 11px; } .right h3{
         text-align: center; border: #48745b 4px solid; border-radius: 4px; background-color: white; font: normal 600 18px "KaiTi_GB2312"; color: #48745b; } /*设置存储内容的样式*/ .content div {
         /*border: 4px solid #48745b; border-radius: 3px;边框样式*/ margin: 5px 2px 0 2px; padding: 10px 0 10px 10px; } .content p {
         margin-top: 5px; font: normal 500 18px "KaiTi_GB2312"; } 
        style> 
         head> <body> <div class="main"> <h1>每日简报
          h1> 
          <a href="https://www.bing.com/search?q=%E6%82%89%E5%B0%BC%E5%A5%A5%E6%9E%97%E5%8C%B9%E5%85%8B%E5%85%AC%E5%9B%AD&form=hpcapt&mkt=zh-cn" target="_blank"> <img src="https://www.bing.com/th?id=OHR.BarcelonaPop_ZH-CN3687855585_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp" alt="Bing每日一图" width=496px> 
           a> <p class="img_des">悉尼奥林匹克公园里的湾标了望台,澳大利亚 (© ai_yoshi/Getty Images)
            p> <div> <div class="header"> <div class="big_line">
             div> <div class="small_line">
              div> 
               div> <div class="body"> <span class="left"> <h1>每日早报
                h1> <h2>NEWS TODAY
                 h2> 
                  span> <span class="right"> <h1>
                 2022年7月12日
                   h1> <h3>
                  星期二
                    h3> 
                     span> 
                      div> <div class="content"> <div>
                       div> 
                        div> 
                         div> 
                          div> 
                           body> 
                            html> 
三、 获取数据 1、 获取图片

调用必应每日一图的接口,获取图片

# !/usr/bin/python3
# -*- coding: UTF-8 -*-
__author__ = "A.L.Kun"
__file__ = "getImg.py"
__time__ = "2022/7/12 11:20"
"""获取到每日图片"""

from requests import get
from fake_useragent import UserAgent


def getResp():
    url = "https://cn.bing.com/HPImageArchive.aspx"  # 这里使用的是bing每日图片链接
    resp = get(url, headers={  # 发送请求
        "user-agent": UserAgent().random,
    }, params={
        "format": "js",  # 返回JSON数据
        "idx": 1,  # 获取前一天的图片
        "n": 1
    })
    # print(resp)
    return resp.json()


def main1():
    src = getResp()
    url_img = "https://www.bing.com" + src["images"][0]["url"]
    title = src["images"][0]["copyright"]
    url_title = src["images"][0]["copyrightlink"]
    return {
        "url_img": url_img,
        "title": title,
        "url_title": url_title
    }  # 返回图片数据


if __name__ == '__main__':
    print(main1())
2、 获取新闻

调用接口,获取每日新闻

# !/usr/bin/python3
# -*- coding: UTF-8 -*-
__author__ = "A.L.Kun"
__file__ = "getInfo.py"
__time__ = "2022/7/12 10:08"
"""获取到每日新闻"""
from requests import get
from fake_useragent import UserAgent


def get_resp():
    url = "https://news.topurl.cn/api"  # 发送请求的接口
    resp = get(url, headers={
   
        "user-agent": UserAgent().random
    }, params={
   
        "count": 20,  # 获取20条新闻
    })
    resp.encoding = resp.apparent_encoding  # 设置编码
    return resp.json()  # 返回JSON数据


def main2():
    data_ = get_resp()
    temp = []
    for i in data_["data"]['newsList']:  # 清洗数据
        temp.append({
   
            "content": i["title"],
        })
    return temp


if __name__ == '__main__':
    print(main2())
3、 制作数据
# !/usr/bin/python3
# -*- coding: UTF-8 -*-
__author__ = "A.L.Kun"
__file__ = "GenHtml.py"
__time__ = "2022/7/12 14:21"
""" 生成HTML页面 """
from getImg import main1
from getInfo import main2  # 导入获取数据
import datetime  # 导入时间库
from pyquery import PyQuery  # 对html文件进行修改


def get_timer():
    """获取今天的时间"""
    week = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
    now = datetime.datetime.now()

    time_ = now.strftime("%Y年%m月%d日")
    week_ = week[int(now.strftime("%w"))]
    return time_, week_

img = main1()
# print(img)
data = main2()
# print(data)
time_, week_ = get_timer()  


def main3():
    html = PyQuery(filename="./templates/index.html")
    # 设置时间
    html(".right h1").text(time_)  # 设置当前时间
    html(".right h3").text(week_)  # 设置第几周
    # 设置图片
    html(".img_des").text(img["title"])  # 设置图片的标题
    html(".main a").attr("href", img["url_title"])  # 设置图片链接
    html(".main a img").attr("src", img["url_img"])  # 设置图片
    # 设置新闻显示
    cont = html(".main .content div")
    for index, item in enumerate(data):
        str_ = f"{
     index + 1}. {
     item['content']}"
        p = " 

%s

"
% str_ cont.append(p) return html.outer_html() # 导出html文件 if __name__ == '__main__': print(main3())
四、 发送邮件
# !/usr/bin/python3
# -*- coding: UTF-8 -*-
__author__ = "A.L.Kun"
__file__ = "main.py"
__time__ = "2022/7/12 15:12"

import sys
from GenHtml import time_, week_, main3
from smtplib import SMTP
from email.mime.text import MIMEText  # 发送文本信息使用的库
from email.header import Header  # 设置请求的头部信息
from email.utils import formataddr  # 格式化
from functools import wraps
import json

f = open("settings.json", "r", encoding="utf-8")
info = json.load(f)
if info["status"] != 200:
    print("JSON数据读取错误!")
    sys.exit(1)  # 如果没有读取到信息
subject = f'{
     time_}新闻(建议使用电脑查看)'  # 设置邮件的标题
html = main3()


def decorate(fun_):
    username = 'liu.zhong.kun@foxmail.com'  # 发送邮件的qq号
    password_pass = 'sadfadsg'  # 授权密码,有的邮箱是使用登录密码
    smtp = SMTP('smtp.qq.com', 587)  # 创建一个SMTP服务器,这里使用qq邮箱
    smtp.starttls()  # 开启tls
    smtp.login(username, password_pass)  # 登录

    @wraps(fun_)
    def func_mail(*args, **kwargs):
        fun_(smtp, username, *args, **kwargs)  # 调用发邮件的函数
        smtp.quit()  # 关闭服务器
        smtp.close()

    return func_mail


@decorate
def mail(smtp, username):
    for receiver_ in info["data"]:
        msgRoot = MIMEText(html, "html", "utf-8")  # 把html信息发送出去
        msgRoot["Subject"] = Header(subject, "utf-8")  # 设置文本标题
        msgRoot['From'] = formataddr(("A.L.Kun", username))  # 设置发件人信息
        msgRoot['To'] = formataddr((receiver_["name"], receiver_["email"]))  # 设置收件人信息
        smtp.sendmail(username, receiver_["email"], msgRoot.as_string())  # 发送邮件
        print(receiver_["email"], ':发送完成')


if __name__ == '__main__':
    mail()

总代码:https://github.com/liuzhongkun1/spider_/tree/master/autosend

本文《Python 发送每日新闻》版权归A-L-Kun所有,引用Python 发送每日新闻需遵循CC 4.0 BY-SA版权协议。


推荐阅读
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • JavaScript在常人看来都是门出不了厅堂的小语言,仅管它没有明星语言的闪耀,但至少网页的闪耀还是需要它的,同时它是一门很实用的语言,本人平时就喜欢拿它来写点实用工具或应用,本文演示用JavaSc ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • PHPMailer邮件类邮件发送功能的使用教学及注意事项
    本文介绍了使用国外开源码PHPMailer邮件类实现邮件发送功能的简单教学,同时提供了一些注意事项。文章涵盖了字符集设置、发送HTML格式邮件、群发邮件以及避免类的重定义等方面的内容。此外,还提供了一些与PHP相关的资源和服务,如传奇手游游戏源码下载、vscode字体调整、数据恢复、Ubuntu实验环境搭建、北京爬虫市场、进阶PHP和SEO人员需注意的内容。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • 表单提交前的最后验证:通常在表单提交前,我们必须确认用户是否都把必须填选的做了,如果没有,就不能被提交到服务器,这里我们用到表单的formname.submit()看演示,其实这个对于我们修炼道 ... [详细]
  • <!DOCTYPEhtml><html><head><metacharsetutf-8><head&g ... [详细]
  • IntheBestPracticestoimprovewebsitePerformancehttp:developer.yahoo.comperformancerules.h ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 移动端常用单位——rem的使用方法和注意事项
    本文介绍了移动端常用的单位rem的使用方法和注意事项,包括px、%、em、vw、vh等其他常用单位的比较。同时还介绍了如何通过JS获取视口宽度并动态调整rem的值,以适应不同设备的屏幕大小。此外,还提到了rem目前在移动端的主流地位。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
  • JS实现一键分享功能
    本文介绍了如何使用JS实现一键分享功能,并提供了2019独角兽企业招聘Python工程师的标准。同时,给出了分享到QQ空间、新浪微博和人人网的链接。 ... [详细]
  • 但有时候,需要当某事件触发时,我们先做一些操作,然后再跳转,这时,就要用JAVASCRIPT来实现这一跳转功能。下面是具体的做法:一:跳转到新页面,并且是在新窗口中打开时:复制代码代码如下:fu ... [详细]
  • ImgettingabugwithInternetExplorer.Theiframedoesnthavecorrectsizeanditisdisplayedo ... [详细]
author-avatar
大胡子姐姐爱油面巾塞肉
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有