Python 撩妹大全之一 邮件系类的小暖男

Python 撩妹大全之一 邮件系类的小暖男

本篇博客将教会大家如何使用python给自己喜欢或者不喜欢的人发有邮件(= = 不喜欢就是邮件轰击, 再来个免责声明:只教撩妹,其他的都是他们干的)
  1. 环境
Python: 3.6.5

IDE: PyCharm Community Edition 
  1. 需要使用到的模块(没有的请手动 pip install 安装)
urllib
beautifulsoup4
time
smtplib
email
  1. 撸码

(1)爬取天气网上面的数据并处理好,方便发送邮件的时候用

import urllib.request
from bs4 import BeautifulSoup
import time

url = "http://www.weather.com.cn/weather/101270101.shtml"
#这是成都的天气信息,根据自己的需求,童鞋们请自行修改url

header = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")  # 设置头部信息
opener = urllib.request.build_opener()  # 修改头部信息
opener.addheaders = [header]         #修改头部信息
request = urllib.request.Request(url)   # 制作请求
response = urllib.request.urlopen(request)   #  得到请求的应答包
html = response.read()   #将应答包里面的内容读取出来
html = html.decode('utf-8')    # 使用utf-8进行编码,不重新编码就会成乱码

#需要了解的同学可以去官网看一下这些方法是怎么实现的
bs = BeautifulSoup(html,'html.parser')
body = bs.body
data = body.find('div',{'id':'7d'})
ul = data.find('ul')
li = ul.find_all('li')
#到这里 天气信息我们就get到了,下面就需要对这些数据进行处理

#简单介绍一下,这个方法有个bug ,emmmm懒得改了 反正又不是我找女朋友。
#你们自行修改,就是日期要出问题
#通过以下方法,我们可以get 日期、天气情况、温度
def get_cd_weather():
    weather_data = []
    for i in li:
        strs = ''
        date = i.find('h1').string
        strs += str(time.strftime('%Y', time.localtime(time.time()))) + '年' + str(time.strftime('%m', time.localtime(time.time()))) + '月'+ date + '\t'
        weather = i.find('p').string
        strs += weather + '\t'
        max_c = i.find('span').string
        min_c = i.find('i').string
        if max_c == None:
            max_c = ''
        if min_c == None:
            min_c = ''
        C = max_c + '\\' + min_c
        # print(date,weather,max_c,min_c)
        strs += C
        weather_data.append(strs)

    return weather_data

(2)发送邮件的基本配置(这里 我们以QQ邮箱为例子)

smtpserver = 'smtp.qq.com'
user_name = '[email protected]'#自己邮箱
password = 'xxxxxxxxx'
#声明一下这个密码不是QQ密码 而是QQ邮箱SMTP服务授权码,自行百度 生成过程.
sender = '[email protected]'#自己邮箱

receiver = ['[email protected]','[email protected]','[email protected]']#需要发送的人邮箱

#通过Header对象编码的文本,包含utf-8编码信息和Base64编码信息
subject = '专属每日天气预报详情 (●ω●) '
subject = Header(subject,'utf-8').encode()

#构造邮件对象MIMEultipart对象
#下面的主题、发件人、收件人、日期显示在邮件页面上
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = '[email protected] <宇宙最可爱的天气报告员(˙ω˙)>'
#多人发送时同样适用
msg['To'] = ';'.join(receiver)
msg['Date'] = str(time.strftime('%Y-%m-%d', time.localtime(time.time())))

weather = get_cd_weather()
html = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>天气预报</title>
</head>
<body>
<center>
<font size="5" font-family: "Arial","Microsoft YaHei","黑体","宋体",sans-serif;>成都近一周的天气情况</font>
</center>
<table border="0" align="center">
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
</table>
</body>
</html>
'''% (weather[0],weather[1],weather[2],weather[3],weather[4],weather[5],weather[6])
text_html = MIMEText(html,'html','utf-8')
msg.attach(text_html)

smtp = smtplib.SMTP_SSL('smtp.qq.com',465)
smtp.login(user_name,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()

(3)完整代码

#coding:utf-8

import urllib.request
from bs4 import BeautifulSoup
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header

url = "http://www.weather.com.cn/weather/101270101.shtml"
header = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")  # 设置头部信息
opener = urllib.request.build_opener()  # 修改头部信息
opener.addheaders = [header]         #修改头部信息
request = urllib.request.Request(url)   # 制作请求
response = urllib.request.urlopen(request)   #  得到请求的应答包
html = response.read()   #将应答包里面的内容读取出来
html = html.decode('utf-8')    # 使用utf-8进行编码,不重新编码就会成乱码

find = []

bs = BeautifulSoup(html,'html.parser')
body = bs.body
data = body.find('div',{'id':'7d'})
ul = data.find('ul')
li = ul.find_all('li')

def get_cd_weather():
    weather_data = []
    for i in li:
        strs = ''
        date = i.find('h1').string
        strs += str(time.strftime('%Y', time.localtime(time.time()))) + '年' + str(time.strftime('%m', time.localtime(time.time()))) + '月'+ date + '\t'
        weather = i.find('p').string
        strs += weather + '\t'
        max_c = i.find('span').string
        min_c = i.find('i').string
        if max_c == None:
            max_c = ''
        elif min_c == None:
            min_c = ''
        C = max_c + '\\' + min_c
        # print(date,weather,max_c,min_c)
        strs += C
        weather_data.append(strs)


#发送邮箱基本配置
smtpserver = 'smtp.qq.com'
user_name = '[email protected]'
password = 'xxxxxxxxxx'
sender = '[email protected]'

receiver = ['[email protected]','[email protected]','[email protected]']

#通过Header对象编码的文本,包含utf-8编码信息和Base64编码信息
subject = '专属每日天气预报详情 (●ω●) '
subject = Header(subject,'utf-8').encode()

#构造邮件对象MIMEultipart对象
#下面的主题、发件人、收件人、日期显示在邮件页面上
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = '[email protected] <宇宙最可爱的天气报告员(˙ω˙)>'
#多人发送时同样适用
msg['To'] = ';'.join(receiver)
msg['Date'] = str(time.strftime('%Y-%m-%d', time.localtime(time.time())))

weather = get_cd_weather()
html = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>天气预报</title>
</head>
<body>
<center>
<font size="5" font-family: "Arial","Microsoft YaHei","黑体","宋体",sans-serif;>成都近一周的天气情况</font>
</center>
<table border="0" align="center">
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
  <tr>
    <td>%s</td>
  </tr>
</table>
</body>
</html>
'''% (weather[0],weather[1],weather[2],weather[3],weather[4],weather[5],weather[6])
text_html = MIMEText(html,'html','utf-8')
msg.attach(text_html)

smtp = smtplib.SMTP_SSL('smtp.qq.com',465)
smtp.login(user_name,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()

(4)部署 (个人能力 手动、自动 选一个吧,反正我自动 嘻嘻)

你需要个服务器 只要是个服务器吧 都应该可以跑(Linux)
没有的同学嘛,算了别找女朋友了(开玩笑的)使用Windows环境下的Pycharm run 一下就是了 只要不嫌麻烦。

1.同样安装Python3.6.5 
安装步骤请参考 https://segmentfault.com/a/1190000015628625

2.通过Xftp 把写好的代码放在服务器指定路径(反正放上去就是了,用上面工具都可以)

3.使用Linux crontab 启用每日定时任务 记住用绝对路径(就是让代码每天自己跑,免得自己动手了,时间自己定 嘻嘻 不许干坏事请)

(5)效果(这里和你们不一样的是我加了图片 你们改一下发送的html )
在这里插入图片描述

最后 有的舔到最后应有尽有,而有的舔到最后一无所有 ,愿所有有情人终为… 算了 溜了溜了 我们下期见
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章