网页转换成图片

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()
'''

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好用的网页转换为图片的工具了。 有人收藏邮票,有人收藏奇石,有人收藏古画,林林总总,无奇不有。在中国,互联网络从出现到现在快二十年了,您有没有从中发现某些收藏的机会呢? 假如您的亲朋好友开设有网站,假如您在一年后,把他所拥有的网站的某些WEB页面以原样方式,把这一年中每一天的变迁呈现在他的面前,您认为他不会万分惊喜么?假如您把对自己有纪念意义的网站的主要页面持之以恒地进行收藏,比如:自己小学、中学或者大学母校的网站进行收藏,那么,若干年以后,当您把这些收藏再呈送给母校,难道不是一件特别有意义的事情么?或者,把自己所在单位的网站,或者某些处于创业期的公司的网站进行收藏,当这些公司若干年后成长为一家大型企业的时候,而当他们的老总看到自己创业期的网站的样子,难道他们不会觉得弥足珍贵么?假如,您需要从网上收集某些证据,而这些证据可能在很少的时候才会出现,那么,您也可以把这视为网页收藏的一个另类应用吧。总之,网页收藏,还是很有价值的。 与传统的纸质出版物相比,网络内容具有某种更高的易逝性。我们现在还能够比较容易地看到几十年、几百年前的原版图书,但是,对于如此众多的WEB网页,随着网站内容频繁地更新或者版面的改版,几分钟、几小时、几天、几月甚至几年前的某一个页面我们还能够复现么?没有有心人,这种复现是不可能的(有人可能会认为百度、Google等搜索引擎也有这些页面的快照,但是,您需要知道:搜索引擎不是每天都会去搜索某一个页面的,通常的时间间隔是一个星期左右,而且只是"可能"会搜集到您关心的WEB页面;更为重要的是,搜索引擎主要关心该页面的文字内容, 动画、图片等内容不会予以保留,特别地,搜索引擎不会关心该页面...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值