python脚本,从mongo取数据发送html格式表格邮件

简介: python脚本,从mongo取数据发送html格式表格邮件

工作需要,我要把我的工作成果每隔三天发送邮件,展示三天的工作情况,所以在linux上写了个脚本,每三天发一次邮件,下面是源代码



coding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from config import *
from mongo_db import MongoDB
import time,datetime
import random
import json
import arrow
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.header import Header
from email import encoders
#我是用当天日期作为表名存入mongo
#链接mongo进行查询
def select_mongo(data):
    pic_mongo = MongoDB(config = mongo_config).getClient().get_database('pic_db').get_collection(data)
    #查询值
    record = pic_mongo.find_one({"name": "log"})
    return record
def querydata():
    res = []
    records = []
    #获取现在的时间  "2018-01-20"
    qq = time.strftime("%Y-%m-%d")
    res.append(qq)
    #获取前两天的时间
    for x in range(1,3):
        now = arrow.get(qq)
        tomorrow = now.replace(days=-x)
        res.append(tomorrow.strftime("%Y-%m-%d"))
    print res
    sendhead = ['上传信息<br>']
    for tim in res:
        record =select_mongo(tim)
        records.append(record)
        sendhead.append('%s<br>' % tim)
    #print records
    tablecontent = '<table border="1" class="imagetable"><caption><h4>数据展示</h4></caption>'
    tablecontent += "<tr>"
    for title in sendhead:
        tablecontent += '<th>'+title+'</th>'
    tablecontent += "</tr>"
    for city_name in records[1]:
        if city_name == '_id' or city_name == 'name':
            pass
        else:
            tablecontent += '<tr>'
            tablecontent += '<td>%s</td>' % city_name
            #print city_name
            for record in records:
                count_sum = record[city_name]
                tablecontent += '<td>%s</td>' % count_sum
            tablecontent += '</tr>'
    tablecontent += '</table>'
    return tablecontent
head='''
<!DOCTYPE html>
<html>
 <head>
     <meta charset="utf-8"> 
     <title>数据展示</title>
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <!-- <meta http-equiv="refresh" content="3600">  -->
  <style type="text/css">
            table.imagetable {
            font-family: verdana,arial,sans-serif;
          font-size:11px;
          color:#333333;
          border-width: 1px;
          border-color: #999999;
          border-collapse: collapse;
            width: 800px;
        }
        table.imagetable th {
            background:#b5cfd2 url('cell-white.jpg');
            border-width: 1px;
          padding: 8px;
          border-style: solid;
          border-color: #999999;
        }
        table.imagetable td {
            background:#dcddc0 url('cell-grey.jpg');
            border-width: 1px;
          padding: 8px;
          border-style: solid;
          border-color: #999999;
            width:150px;
        }
        table.imagetable td.tou {
            background:#dcddc0 url('cell-grey.jpg');
            border-width: 1px;
          padding: 8px;
          border-style: solid;
          border-color: #999999;
            width:800px;
        }
  </style>
 </head>
 <body>
'''
tail='''
 </body>
</html>
'''
res = querydata()
msg = MIMEMultipart()
content=head+res+tail
msgtitle = '【图片展示】'
text=MIMEText(content,'html','utf-8')
msg.attach(text)
msg['from'] = 'me'
msg['to'] = '小'
msg['Subject'] = msgtitle
s = smtplib.SMTP('smtp.exmail.qq.com')
s.login('邮箱', 'smtp码')
s.sendmail('邮箱', ['收件人邮箱'], msg.as_string())
print('success')
s.close()
if __name__ == "__main__":
    content = head+res+tail
    print content


目录
相关文章
|
14天前
|
存储 数据采集 JSON
你知道吗?html_table可以提取的不止是表格
`html_table` 是一种强大的工具,不仅用于HTML表格解析,还在现代爬虫技术中发挥重要作用。它可以提取、整合、分析和传输多种类型的关键数据。本文从四个方面探讨其功能:关键数据提取(如财经网站的股票信息)、零散信息整合(如电商网站的产品详情)、数据对比分析(如手机性能参数对比)和数据存储与传输(如转换为CSV/JSON格式)。通过Python代码示例,展示了如何利用代理IP、多线程和自定义请求头提高爬虫效率,实现对复杂网页数据的全面抓取和利用。
32 12
你知道吗?html_table可以提取的不止是表格
|
1月前
|
数据处理 索引 Python
用Python实现数据录入、追加、数据校验并生成表格
本示例展示了如何使用Python和Pandas库实现学生期末考试成绩的数据录入、追加和校验,并生成Excel表格。首先通过`pip install pandas openpyxl`安装所需库,然后定义列名、检查并读取现有数据、用户输入数据、数据校验及保存至Excel文件。程序支持成绩范围验证,确保数据准确性。
87 14
|
1月前
|
数据采集 前端开发 数据挖掘
利用 html_table 函数轻松获取网页中的表格数据
本文介绍了如何使用 R 语言中的 `html_table` 函数结合代理 IP 技术,轻松提取网页表格数据并规避反爬机制。通过设置代理和请求头,示例代码展示了如何从 58 同城采集租房信息并保存为 CSV 文件。该方法适用于需要频繁采集数据的场景,确保数据采集的高效和稳定性。
利用 html_table 函数轻松获取网页中的表格数据
|
2月前
|
XML 数据采集 数据格式
Python 爬虫必备杀器,xpath 解析 HTML
【11月更文挑战第17天】XPath 是一种用于在 XML 和 HTML 文档中定位节点的语言,通过路径表达式选取节点或节点集。它不仅适用于 XML,也广泛应用于 HTML 解析。基本语法包括标签名、属性、层级关系等的选择,如 `//p` 选择所有段落标签,`//a[@href=&#39;example.com&#39;]` 选择特定链接。在 Python 中,常用 lxml 库结合 XPath 进行网页数据抓取,支持高效解析与复杂信息提取。高级技巧涵盖轴的使用和函数应用,如 `contains()` 用于模糊匹配。
|
2月前
|
移动开发 JavaScript 前端开发
html table+css实现可编辑表格的示例代码
html table+css实现可编辑表格的示例代码
105 12
|
1月前
|
机器学习/深度学习 前端开发 数据处理
利用Python将Excel快速转换成HTML
本文介绍如何使用Python将Excel文件快速转换成HTML格式,以便在网页上展示或进行进一步的数据处理。通过pandas库,你可以轻松读取Excel文件并将其转换为HTML表格,最后保存为HTML文件。文中提供了详细的代码示例和注意事项,帮助你顺利完成这一任务。
49 0
HTML 表格4
HTML 表格标签用于创建和布局表格。主要标签包括:`&lt;table&gt;` 定义表格,`&lt;th&gt;` 定义表头,`&lt;tr&gt;` 定义行,`&lt;td&gt;` 定义单元格,`&lt;caption&gt;` 定义标题,`&lt;colgroup&gt;` 和 `&lt;col&gt;` 定义列属性,`&lt;thead&gt;`、`&lt;tbody&gt;` 和 `&lt;tfoot&gt;` 分别定义表格的页眉、主体和页脚。
|
2月前
|
JavaScript API 开发工具
<大厂实战场景> ~ Flutter&鸿蒙next 解析后端返回的 HTML 数据详解
本文介绍了如何在 Flutter 中解析后端返回的 HTML 数据。首先解释了 HTML 解析的概念,然后详细介绍了使用 `http` 和 `html` 库的步骤,包括添加依赖、获取 HTML 数据、解析 HTML 内容和在 Flutter UI 中显示解析结果。通过具体的代码示例,展示了如何从 URL 获取 HTML 并提取特定信息,如链接列表。希望本文能帮助你在 Flutter 应用中更好地处理 HTML 数据。
146 1
HTML 表格3
HTML 表格的表头使用 `&lt;th&gt;` 标签定义,通常以粗体居中显示。
|
6月前
|
数据挖掘 Shell 测试技术
怎么用Python解析HTML轻松搞定网页数据
**Python解析HTML摘要** 本文介绍了使用Python处理HTML的常见需求,如数据提取、网络爬虫和分析,并讨论了三种解析方法。正则表达式适用于简单匹配,但对复杂HTML不理想;BeautifulSoup提供简单API,适合多数情况;lxml结合XPath,适合处理大型复杂文档。示例展示了如何用这些方法提取链接。
194 2

热门文章

最新文章