Python
python# -*- coding: utf-8 -*-
import datetime
import json
import re
import time
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from pprint import pprint
import json
import requests
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# EmailReceivers = ['[email protected]'] # 邮件收件人
EmailReceivers = ["[email protected]", '[email protected]', "[email protected]",
"[email protected]"] # 邮件收件人
localFileName = "1234.xlsx"
def send_email_to_d(text, receivers):
# 第三方 SMTP 服务
mail_host = "smtp.exmail.qq.com" # 设置服务器
mail_user = "[email protected]" # 用户名
mail_pass = "qqqqqqqqqqq" # 口令
sender = '[email protected]'
message = MIMEMultipart('mixed') # 邮件内容,mixed为混合形式可传附件
message['From'] = Header("[email protected]", 'utf-8')
message['To'] = Header(";".join(EmailReceivers), 'utf-8')
message['Subject'] = Header('和风API调用费用统计', 'utf-8')
# print(text) # 邮件文本内容编辑
text_plain = MIMEText(text, 'plain', 'utf-8')
message.attach(text_plain) # 实例化文本
docxpart = MIMEApplication(open(localFileName, 'rb').read())
docxpart.add_header('Content-Disposition', 'attachment', filename=localFileName)
message.attach(docxpart) # 要发送的exl实例化
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
class scrapyler:
def __init__(self):
pass
def getCookie(self):
userName = "xxxx"
userPassword = "xxxxx"
caps = {
'browserName': 'chrome',
'version': '',
'platform': 'ANY',
'goog:loggingPrefs': {'performance': 'ALL'}, # 记录性能日志
'goog:chromeOptions': {'extensions': [], 'args': ['--headless']} # 无界面模式
}
# driver = webdriver.Chrome(executable_path='chromedriver.exe', desired_capabilities=caps)
driver = webdriver.Chrome(executable_path='/home/pythonPackage/chromedriver', desired_capabilities=caps)
driver.get('https://id.qweather.com/#/login?redirect=https%3A%2F%2Fconsole.qweather.com%2F#/expenses')
browser = driver
print("网页响应中。。。")
wait = WebDriverWait(browser, 30)
browser.find_element(by="xpath",
value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[3]/form/div[1]/div/div/input""").clear()
browser.find_element(by="xpath",
value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[3]/form/div[1]/div/div/input""").send_keys(
userName)
browser.find_element(by="xpath",
value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[3]/form/div[2]/div/div/input""").clear()
browser.find_element(by="xpath",
value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[3]/form/div[2]/div/div/input""").send_keys(
userPassword)
browser.find_element(by="xpath",
value=r"""//*[@id="app"]/div/div/div[2]/div/div[3]/div/div[5]/button""").click()
time.sleep(5)
balance = float(browser.find_element(by="xpath",
value=r"""//*[@id="page"]/section/main/div/div/div[2]/div[1]/div/div[3]/div[1]/div/span[2]""").text)
logs = driver.get_log("performance")
cookies = None
for i in logs:
if re.search(r'''"cookie":"[\s\S]{10,200}__HE_CORE__=[\s\S]{10,200}JSESSIONID=[0-9A-Za-z]{10,200}"''',
str(i)) is not None:
cookies = re.search(
r'''"cookie":"[\s\S]{10,200}__HE_CORE__=[\s\S]{10,200}JSESSIONID=[0-9A-Za-z]{10,200}"''',
str(i)).group()
break
assert cookies is not None
cookie = cookies[10:-1]
browser.quit()
# browser.close()
return cookie, balance
def getAppBinding(self, cookie):
prog = requests.get(headers={"cookie": cookie}, url="https://console.qweather.com/v1/rest/app").json()["data"]
idname = {lt["name"]: lt["id"] for lt in prog}
print(idname)
return idname
def getPurchaseHistory(self, cookie, appId: str,
yearmonth: str): # "https://console.qweather.com/v1/rest/fee/app/95491/type/2?date=2021-11"
url = f"https://console.qweather.com/v1/rest/fee/app/{appId}/type/2?date={yearmonth}"
prog = requests.get(headers={"cookie": cookie}, url=url).json()["data"]
print(prog)
return prog
def run(self):
year = datetime.datetime.now().year - 1 if datetime.datetime.now().month - 1 == 0 else datetime.datetime.now().year
month = 12 if datetime.datetime.now().month - 1 == 0 else datetime.datetime.now().month - 1
yearmonthstr = str(year) + "-" + str(month).zfill(2)
cookie, balance = self.getCookie()
time.sleep(0.5)
idname = self.getAppBinding(cookie)
time.sleep(0.5)
resultdf = []
need = ["森林消防", "二块板天气数据库专用", "环保生产"]
for nd in need:
prog = self.getPurchaseHistory(cookie, idname[nd], yearmonthstr)
resultdf.append(pd.DataFrame(prog))
df = pd.concat(resultdf)
df["cost"] = df["cost"].apply(float)
totalMonney = df["cost"].sum()
df.to_excel(localFileName)
return totalMonney, need, balance
if __name__ == '__main__':
# 爬虫获取 总金额,行业名列表,本地文件“和风API调用费用明细.xlsx”
scrapyler = scrapyler()
totalMonney, need, balance = scrapyler.run()
# 发邮件
if datetime.datetime.now().day == 6 or balance <= 5:
year = datetime.datetime.now().year - 1 if datetime.datetime.now().month - 1 == 0 else datetime.datetime.now().year
month = 12 if datetime.datetime.now().month - 1 == 0 else datetime.datetime.now().month - 1
yearmonthstr = str(year) + "-" + str(month).zfill(2)
send_email_to_d(f"和风API调用当前余额还剩余 {balance} 元。\n"
f"\n"
f"\n"
f"和风API调用费用统计见附件,{yearmonthstr}月份总消费金额 {totalMonney} 元。\n"
f"此统计只包含这些项目行业{str(need)}。\n"
f"\n"
f"\n"
f"--------------------------------------------------------------------------"
f"\n"
f"此邮件是跑在 http://10.10.90.54:8888/dolphinscheduler/ui/#/resource/file/edit/280 的定时脚本。\n"
f"每月6号发一次邮件。每天检测一次余额,余额低于5元也会发本邮件。"
f"和风api登录地址: https://id.qweather.com/#/login?redirect=https%3A%2F%2Fconsole.qweather.com%2F#/expenses"
f"\n"
f" "
,
EmailReceivers)
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!