getdata.py
# encoding: utf8
import requests
from requests.auth import AuthBase
from requests.auth import HTTPBasicAuth
from mail import EmailManager
import pdfkit
from pdf2image import convert_from_path, convert_from_bytes
import tempfile
import urllib3
import os
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
s = requests.session()
#s.cookies = CookieJar()
class dailyrp():
def getdata(self):
url1 = "https://9.119.72.67/"
htmlget = s.get(url1,verify=False)
html1 = htmlget.text
cookies = htmlget.cookies
auth = HTTPBasicAuth("shenf@cn.*.com".encode('utf-8'),"dfsdf20")
#csrfmtokenre = re.compile("name=\"csrfmiddlewaretoken\".*")
#csrfmtoken = re.findall(csrfmtokenre, html1)[0].split("value=")[1].split("\"")[1]
response = s.get("https://9.119.72.67/clients/msf/", auth=auth, verify=False)
responsetxt = response.text
index = s.get("https://9.119.72.67/clients/msf/morning_checks/index.php", auth=auth, verify=False)
indextxt = index.text
output = open("/home/cictest/mfs_HCreport/index.html", 'w')
output.write(indextxt)
output.close()
os.popen('bash /home/cictest/mfs_HCreport/edindex.sh')
'''
data = {}
data['csrfmiddlewaretoken'] =
html2 =
'''
def pdfimg(self):
try:
options = {
#'page-size': 'Letter',
#'margin-top':'0.75in',
#'margin-right':'0.75in',
#'margin-bottom':'0.75in',
#'margin-left':'0.75in',
}
pdfkit.from_file('/home/cictest/msf_HCreport/index.html', '/home/cictest/msf_HCreport/out.pdf', options=options)
except:
pass
with tempfile.TemporaryDirectory() as path:
images_from_path = convert_from_path('/home/cictest/msf_HCreport/out.pdf', dpi=150, output_folder=path)
print(images_from_path)
i = 1
for img in images_from_path:
img.save("/home/cictest/msf_HCreport/ilcimg{0}".format(i), "JPEG")
i += 1
morhcrp = dailyrp()
morhcrp.getdata()
morhcrp.pdfimg()
EmailManager().sendShowAnnex_email()
参考pdf2image: https://pypi.org/project/pdf2image/
参考pdfkit: https://pypi.org/project/pdfkit/
对网页进行处理
#!/bin/bash
sed -i -e '/id=\"masthead\"/,/stop masthead/d' -e '/Backup Status Information/, /stop sidebar/d' -e '/start navigation/,/stop navigation/d' -e '/id=\"breadcrumbs\"/d' -e '/id=\"date-stamp\"/d' /home/cictest/msf_HCreport/index.html
mail.py:
import smtplib
from email import encoders
#from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.mime.image import MIMEImage
class EmailManager():
def sendShowAnnex_email(self):
# 邮件对象:
message = MIMEMultipart()
emails = []
message['from'] = 'linx1@cn.abc.com'
message['to'] = 'shenf@cn.abc.com'
message['cc'] = 'gbanc@cn.abc.com'
emails = ['linx1@cn.abc.com', 'shenf@cn.abc.com', 'gbanc@cn.abc.com']
#emails = ['shenf@cn.abc.com']
message['subject'] = Header(u'Mfs daily morning HC report', 'utf-8').encode()
smtp_server = "localhost"
server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25
with open('/home/cictest/mfs_HCreport/ilcimg1', 'rb') as f:
mime_data = f.read()
img = MIMEImage(mime_data)
img.add_header('Content-ID', '<0>')
msg = ''' Hi All, <br> Here is the morning HC report for Mfs. <br> ''' + ''' <img src="cid:0"> '''
message.attach(MIMEText(msg, 'html', 'utf-8'))
message.attach(img)
server.sendmail(message['from'], emails, message.as_string())
server.quit()
'''
if __name__== '__main__':
em = EmailManager()
em.sendShowAnnex_email()
'''