Skip to content

Commit ebc3c52

Browse files
author
xuming06
committed
add send email
1 parent 0aa2171 commit ebc3c52

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

tool/send_email.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author:XuMing([email protected])
4+
@description:
5+
"""
6+
7+
import smtplib
8+
from email.header import Header
9+
from email.mime.multipart import MIMEMultipart
10+
from email.mime.text import MIMEText
11+
from email.utils import parseaddr, formataddr
12+
13+
import requests
14+
from bs4 import BeautifulSoup
15+
from lxml import etree
16+
17+
# 常量
18+
from_addr = '[email protected]'
19+
password = 'xx'
20+
21+
smtp_server = 'smtp.126.com'
22+
url = 'http://wufazhuce.com/'
23+
24+
# 标记
25+
isSent = False
26+
27+
28+
# 编码转换方法
29+
def _format_addr(s):
30+
name, addr = parseaddr(s)
31+
return formataddr((Header(name, 'utf-8').encode(), addr))
32+
33+
34+
# 邮件方法
35+
def sendEmail(text, img, title, story, to_addr):
36+
msg = MIMEMultipart()
37+
38+
msg['From'] = _format_addr(u'XuMing <%s>' % from_addr)
39+
msg['To'] = _format_addr(u'管理员 <%s>' % to_addr)
40+
msg['Subject'] = Header(u'The One ' + title, 'utf-8').encode()
41+
42+
msg.attach(MIMEText('<html><body><div style="text-align: center;"><p><img src="' + img + '"></p></div>' +
43+
'<p style="text-align:center;\"> <br /><br /><strong><span style="font-size:14px; text-align: center;\">' + text +
44+
'</span></p><br /><br /><br /><br /><br />' + story + '</body></html>',
45+
'html', 'utf-8'))
46+
47+
server = smtplib.SMTP(smtp_server, 25)
48+
server.set_debuglevel(1)
49+
server.login(from_addr, password)
50+
server.sendmail(from_addr, [to_addr], msg.as_string())
51+
server.quit()
52+
53+
54+
def http(url):
55+
html = requests.get(url).text
56+
57+
page = etree.HTML(html.lower())
58+
# print(page)
59+
60+
61+
soup_main = BeautifulSoup(html)
62+
# "一个"的文字
63+
div = soup_main.find_all("div", {"class": "fp-one-cita"})
64+
text = div[0].a.text
65+
# print(text)
66+
67+
# “一个”的图片地址
68+
img_list = soup_main.find_all("img", {"class": "fp-one-imagen"})
69+
imgUrl = img_list[0].get('src')
70+
# print(imgUrl)
71+
72+
# "一个"的标题
73+
title_list = soup_main.find_all("p", {"class": "titulo"})
74+
title = str(title_list[0].text)
75+
print(title)
76+
77+
# title = title.replace("VOL.","")
78+
# # “一个”的文章vol.1132#articulo'
79+
# url_stroy = 'http://wufazhuce.com/ariticle/' + title
80+
# # http://wufazhuce.com/article/1326
81+
82+
# 得到文章的地址 用Xpath方法
83+
url_story = page.xpath("//*[@id=\"main-container\"]/div[1]/div[2]/div/div/div[1]/div/p[2]/a/@href")
84+
print(url_story[0])
85+
86+
soup_stroy = BeautifulSoup(requests.get(url_story[0]).text)
87+
stroy_content = str(soup_stroy.find("div", {"class": "articulo-contenido"}))
88+
89+
stroy_title = str(soup_stroy.find("h2", {"class": "articulo-titulo"}))
90+
91+
stroy = stroy_title + stroy_content
92+
93+
for addr in to_addr:
94+
sendEmail(text, imgUrl, title, stroy, addr)
95+
96+
97+
http(url)
98+
exit()

0 commit comments

Comments
 (0)