文章定时自动采集(关于获取文章自动发送到邮箱,这类需求其实可以写在前面 )

优采云 发布时间: 2022-02-19 03:27

  文章定时自动采集(关于获取文章自动发送到邮箱,这类需求其实可以写在前面

)

  写在前面

  关于让文章自动发到邮箱,这种需求其实可以写几个网站,完成博客园,搞定CSDN,搞定掘金,搞定其他,网站很多哈~哈哈

  让我们从博客园开始。基本需要,在python部分下获取新的文章,每60分钟发送一次。时间太短估计出新的博客不多了~

  抓取的页面就是这个

  https://www.cnblogs.com/cate/python

  需求排序 获取指定页面的所有文章,记录文章的相关信息,并记录最后一个文章的时间,发送文章到指定邮箱,并更新最后一个文章查看@k7@实际编码会话中需要导入的模块>

  模块列表

  import requests

import time

import re

import smtplib

from email.mime.text import MIMEText

from email.utils import formataddr

from email.header import Header

from email.mime.application import MIMEApplication

from email.mime.multipart import MIMEMultipart

  初始化基础数据

   # 初始化数据

def __init__(self):

self.start_url = "https://www.cnblogs.com/cate/python"

self.headers = {

"user-agent": "Mozilla/..... Safari/537.36",

"referer": "https://www.cnblogs.com/cate/python/"

}

self.pattern = r'[\s\S.]*?(.*?)[\s\S.]*?[\s\S.]*?(.*?)([\s\S.]*?)'

self.last_blog_time = 0

self.need_send_articles = []

  参数说明

  解析博客页面内容

  涉及的代码很多,重点我会写相应的注释

   # 解析网页内容

def get_articles(self):

try:

# 正常的数据获取

res = requests.get(self.start_url,headers=self.headers,timeout=3)

except Exception as e:

print("error %s"% e)

time.sleep(3)

return self.get_articles() # 重新发起请求

html = res.text

# 这个地方的正则表达式是考验你正则功底的地方了

all = re.findall(self.pattern,html)

# 判断,如果没有新文章

last_time = self.change_time(all[0][3].strip().replace("发布于 ", ""))

if last_time self.last_blog_time):

self.need_send_articles.append({

"url":item[0],

"title":item[1],

"author":item[2],

"time":public_time

})

# 文章获取完毕,更新时间

self.last_blog_time = last_time

##### 测试输出

print(self.need_send_articles)

print("现在文章的最后时间为",self.last_blog_time)

##### 测试输出

  将时间字符串转换为时间戳

  使用时间戳可以直接比较大小,非常方便

   def change_time(self,need_change_time):

'''

# 时间的转换

:param need_change_time:

:return:返回时间戳

'''

time_array = time.strptime(need_change_time, "%Y-%m-%d %H:%M")

time_stamp = int(time.mktime(time_array))

return time_stamp

  电子邮件发送链接

  本博客使用QQ邮箱发送

  QQ邮箱文章发来的一些参考资料,我列一下,方便大家查阅

  参考文章

# https://blog.csdn.net/qiye005/article/details/80789666

# https://blog.csdn.net/Momorrine/article/details/79881251

# https://www.cnblogs.com/lovealways/p/6701662.html

# https://www.cnblogs.com/yufeihlf/p/5726619.html

  因为我用的是QQ邮箱,有些地方设置比较麻烦,发短信要20毛钱。建议你用其他邮箱,设置都一样~~

  发送电子邮件 send_email 函数

  看了上面的文章就可以写对应的email了,很简单

  QQ邮箱是经过SSL认证的邮箱系统,所以要使用QQ邮箱发送邮件,需要创建一个SMTP_SSL对象而不是SMTP对象

   # 发送邮件

def send_email(self,articles):

smtp = smtplib.SMTP_SSL() # 这个地方注意

smtp.connect("smtp.qq.com",465)

smtp.login("860866679@qq.com", "授权码")

sender = '860866679@qq.com'

receivers = ['找个自己的其他邮箱@163.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# 完善发件人收件人,主题信息

message = MIMEMultipart()

message['From'] = formataddr(["博客采集器", sender])

message['To'] = formataddr(["hi,baby", ''.join(receivers)])

subject = '你有新采集到的文章清单'

message['Subject'] = Header(subject, 'utf-8')

# 正文部分

html = ""

for item in articles:

html+=("<p><a href=&#39;{url}&#39;>{title}</a>--文章作者{author}--发布时间{time}".format(title=item["title"],url=item["url"],author=item["author"],time=item["time"]))

textmessage = MIMEText(&#39;

  新采集到的文章清单&#39; +html,

&#39;html&#39;, &#39;utf-8&#39;)

message.attach(textmessage)

# 发送邮件操作

smtp.sendmail(sender, receivers, message.as_string())

smtp.quit()</p>

  收到的电子邮件

  收到邮件的那一刻,心情就很幸福了~

  

  部署到服务器

  最后一步,如果要继续获取,那就找个服务器,然后部署,有兴趣的博主,继续研究~

  

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线