你想收到hostloc每日热帖的邮件么?

你想收到hostloc每日热帖的邮件么?-夏普博客

碎碎念

掐指一算,混loc的日子应该也快一年了,刚开始还几乎每天都上论坛上翻翻帖子,可后来到现在越来越少上了,但也还会隔一段时间猛刷上个把小时,今天闲来无事写了个小爬虫用来每日推送loc的热帖。

代码

#!/usr/bin/env python3
# coding = 'utf-8'
"""
@Time    : 2020/3/26-18:51
@Author  : sharp
@FileName: hostloc_hot.py
@Software: PyCharm
@Blog    :https://www.sharpgan.com/
"""

import time
import smtplib
import requests
from bs4 import BeautifulSoup
from email.mime.text import MIMEText

HOST = 'smtp.qq.com'
PORT = 587
SENDER = '@qq.com'
RECEIVER = '@qq.com'
PWD = ''


def mail_send(subject, mail_body):
    try:
        msg = MIMEText(mail_body, 'plain', 'utf-8')
        msg['Subject'] = subject
        msg['From'] = SENDER
        msg['To'] = RECEIVER
        s = smtplib.SMTP(HOST, PORT)
        s.debuglevel = 0
        s.login(SENDER, PWD)
        s.sendmail(SENDER, RECEIVER, msg.as_string())
        s.quit()
    except smtplib.SMTPException as e:
        print(str(e))


def main(arg1, arg2):
    prefix = 'https://www.hostloc.com/'
    url = prefix + 'forum.php?mod=forumdisplay&fid=45&filter=hot'
    current_date = time.strftime("%Y-%m-%d", time.localtime())
    content = requests.get(url).text
    soup = BeautifulSoup(content, 'html5lib')
    item_all_list = soup.select('th.new')
    items_title_list = []
    for item in item_all_list:
        title = item.select_one('a.s.xst').get_text()
        href = item.select_one('a.s.xst').get('href')
        item_url = prefix + href
        items_title_list.append(title + '\n' + item_url)
    mail_send(subject=current_date + ' ' + 'Hostloc今日热帖',
              mail_body='\n\n'.join(items_title_list))
    print('成功发送了一封邮件!')

如何使用和部署请参考本站的这篇文章,注意上述代码在pip安装依赖的时候只需要安装`beautifulsoup4`就行了,其它的就不用安装了。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注