核心内容

1.学习使用email和smtplib库

2.利用requests爬取网页内容

3.分析正则匹配式

Code

import smtplib
from email.mime.text import MIMEText
import requests
import re


def go_work(name):
    dic = {'白羊座': 'http://astro.sina.com.cn/fate_day_Aries/',
           '金牛座': 'http://astro.sina.com.cn/fate_day_Taurus/',
           '双子座': 'http://astro.sina.com.cn/fate_day_Gemini/',
           '巨蟹座': 'http://astro.sina.com.cn/fate_day_Cancer/',
           '狮子座': 'http://astro.sina.com.cn/fate_day_leo/',
           '处女座': 'http://astro.sina.com.cn/fate_day_Virgo/',
           '天秤座': 'http://astro.sina.com.cn/fate_day_Libra/',
           '天蝎座': 'http://astro.sina.com.cn/fate_day_Scorpio/',
           '射手座': 'http://astro.sina.com.cn/fate_day_Sagittarius/',
           '魔羯座': 'http://astro.sina.com.cn/fate_day_Capricorn/',
           '水瓶座': 'http://astro.sina.com.cn/fate_day_Aquarius/',
           '双鱼座': 'http://astro.sina.com.cn/fate_day_Pisces/'}
    # 获取星座对应地址
    path = dic[name]
    # 爬取信息
    response = requests.get(path)
    # 将爬回的内容解码成utf-8格式
    html = response.text
    html = html.encode("ISO-8859-1")
    html = html.decode("utf-8")

    # 一系列的正则匹配式
    pattern_lucky = r"今日幸运值</td>\s*<td>(.*?)</td>"
    pattern_love = r"爱情指数</td>\s*<td>(.*?)</td>"
    pattern_work = r"工作指数</td>\s*<td>(.*?)</td>"
    pattern_fortune = r"财运指数</td>\s*<td>(.*?)</td>"
    pattern_health = r"健康指数</td>\s*<td>(.*?)</td>"
    pattern_lucky_number = r"幸运数字</td>\s*<td>(.*?)</td>"
    pattern_coincidence_constellation = r"贵人星座</td>\s*<td>(.*?)</td>"
    pattern_important_weather = r'今日重要天象</td>\s*<td colspan="3">(.*?)</td>'
    pattern_evaluate = r'精评</td>\s*<td colspan="3">(.*?)</td>'
    pattern_explain = r'详述</td>\s*<td colspan="3" style="line-height: 25px;">(.*?)</td>'

    # 正则匹配结果
    lucky_value = re.findall(pattern_lucky, html)
    love_value = re.findall(pattern_love, html)
    work_value = re.findall(pattern_work, html)
    fortune_value = re.findall(pattern_fortune, html)
    health_value = re.findall(pattern_health, html)
    lucky_number_value = re.findall(pattern_lucky_number, html)
    coincidence_constellation_value = re.findall(pattern_coincidence_constellation, html)
    important_weather_value = re.findall(pattern_important_weather, html)
    evaluate_value = re.findall(pattern_evaluate, html)
    explain_value = re.findall(pattern_explain, html)

    # 生成发送信息的内容
    result = {"今日幸运值": lucky_value, "爱情指数": love_value, "工作指数": work_value, "财运指数": fortune_value,
              "健康指数": health_value, "幸运数字": lucky_number_value,
              "契合星座": coincidence_constellation_value,
              "今日重要天象": important_weather_value, "精评": evaluate_value,
              "详述": explain_value
              }
    return result


def send_email(name, receivers):
    result = go_work(name)
    # SMTP服务器地址
    # 下面是以126邮箱为例,qq邮箱就是smtp.qq.com,163邮箱smtp.163.com
    mail_host = 'smtp.126.com'

    # 填写你的邮箱名称
    mail_user = 'abcd'
    # 填写你的授权码,授权码如何获得见下
    mail_pass = 'xxxxxxxxxxx'
    # 填写你的邮箱地址
    sender = 'abcd@126.com'

    # 防止手机端不会自动换行,用html语言将每段话加上换行符
    content = ""
    for key in result:
        content += str(key) + ":" + str(result[key][0]) + "<br/>" + "\n"

    # 创建一个MIMEText对象    
    message = MIMEText(str(content), 'plain', 'utf-8')
    # 邮件主题可以自己修改
    message['Subject'] = str(name) + '今日运势'
    # 邮件发送者
    message['From'] = sender
    # 邮件接收者
    message['To'] = receivers[0]

    try:
        # 创建SMTP实例
        smtpobj = smtplib.SMTP()
        # 连接邮箱服务器,端口号默认为25
        # 默认很可能会失败,端口号具体内容需要查询邮件服务提供商
        smtpobj.connect(mail_host, 25)
        # 登录
        smtpobj.login(mail_user, mail_pass)
        # 发送
        smtpobj.sendmail(
            sender, receivers, message.as_string()
        )
        # 退出
        smtpobj.quit()
        print("success")
    except smtplib.SMTPException as e:
        print("error", e)


if __name__ == "__main__":
    # 填写目标星座
    name = "白羊座"
    # 在这里填写接收邮件的地址
    # 收件人是一个列表,可以放多个收件人地址
    # 如['a@qq.com','b@163.com','c@126.com']
    receivers = ['xxxxxxxxxx@xxx.com']
    send_email(name, receivers)

获取SMTP授权码

以qq邮箱为例

先进入设置-》帐户页面找到入口,按照以下流程操作。

1.点击“开启”

2.验证密保

3.获取授权码
注意保存该授权码,点击确定后就无法查看了

预览效果

电脑端

手机端

Tips

1.由于该程序只需要手动修改一次收件人、发件人、授权码、端口等参数(若后续不修改收件人),所以打包成.exe可执行文件反而更加不方便使用。若想打包为.exe文件,一个建议:可以将参数信息存储到本地,然后修改代码去读取这些信息,后续想要修改信息只需要修改本地参数即可,无需再次修改程序。

2.可以将此程序添加到定时任务中来每天按时发送,详情参考:如何设置python程序定时执行?