Python 撩妹大全之一 郵件系類的小暖男
本篇博客將教會大家如何使用python給自己喜歡或者不喜歡的人發有郵件(= = 不喜歡就是郵件轟擊, 再來個免責聲明:只教撩妹,其他的都是他們乾的)
- 環境
Python: 3.6.5
IDE: PyCharm Community Edition
- 需要使用到的模塊(沒有的請手動 pip install 安裝)
urllib
beautifulsoup4
time
smtplib
email
- 擼碼
(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 )