如何使用 python 从我的控制台捕获所有内容并通过电子邮件发送?

How can I capture everything from my console using python and email it?

我正在编写运行计划作业的代码,并在 chromedriver 上截屏并通过电子邮件发送。我正在尝试找到一种方法来将控制台输出捕获到一个文件中,然后通过电子邮件将其发送出去。下面是调度程序和屏幕截图模块的代码片段。

这是我的调度程序,

import schedule
import time
from datetime import datetime
from screenshot import screenshot

def job():
    print("Launching scresnshot")
    screenshot()

我的截图代码,

import time
from Screenshot import Screenshot_Clipping
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from email_it import email_it
from environmental_variables import environmental_variables
from error_alert_email import error_alert_email
from selenium import webdriver

def screenshot():

    ob=Screenshot_Clipping.Screenshot()
    
    chrome_options = Options()
    
    chrome_options.add_argument('--start-maximized')    
    chrome_options.add_argument('--start-fullscreen')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    
    driver = webdriver.Chrome(executable_path = r"C:\Users\me\Documents\Projects\chromedriver.exe")
    print('Confirming visibility...')
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, visibility_element)))
    
    print('taking screenshot...')
    img_url=ob.full_Screenshot(driver, path = path, image_name = label)

    print('closing driver...')    
    driver.close()

screenshot()

我的控制台输出

PS: C:\Users\Documents\Projects\> python .\src\scheduler.py

DevTools listening on ws://124.1.0.1:76532/devtools/browser/785b0a24-7638-4b2d-a2da-ae62a65891f6
Opening url...
[ERROR:device_event_log_impl.cc(214)] [15:07:41.359] USB: usb_device_handle_win.cc:1054 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
Logging in...
Confirming visibility...
Taking screenshot...
Sending mail...
Message sent!
Closing driver...

您可以做的一件事是将所有控制台输出路由到一个文本文件中,然后在程序完成后通过电子邮件发送文本文件的内容运行。我将在下面添加一些关于如何将所有打印语句定向到文本文件的代码,对于电子邮件发送部分,有一些很好的 python 库可以使发送电子邮件变得非常简单。 Here 是一篇关于该主题的好文章。

sys.stdout = open("test.txt", "w")
print("Hello World")
sys.stdout.close()

你可以试试这个。它是上述方法和日志记录的混合体!

import logging

stdout_ = sys.stdout
sys.stdout = open("logFiles.txt", "w")
e_type, e_val, e_tb = sys.exc_info()
traceback.print_exception(e_type, e_val, e_tb, file = sys.stdout)

root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)

def job():
    print("Launching scresnshot")
    screenshot()
    sys.stdout = stdout_
    sys.stdout.close()