gpt4 book ai didi

python - 读取 MHTML 文件

转载 作者:行者123 更新时间:2023-12-03 17:24:56 25 4
gpt4 key购买 nike

我的本地文件夹中有一个 mhtml 文件存储在 file_path 中,其中包含用户事件日志。我可以使用以下代码打开它:

with open(file_path, 'r') as fp:
message = email.message_from_file(fp)
for part in message.walk():
if (part.get_content_type() == "text/html"):
soup = BeautifulSoup(part.get_payload(decode=False), 'html.parser')
parsed_data = soup.prettify()
print(parsed_data)

下面给出的是我使用上述代码得到的部分输出:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<link href="main.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
function zoomToggle(num)
{
var img = document.getElementById("ss-" + num);

if (img.className == "screenshot") {
img.className = "screenshot-thumb";
}
else {
img.className = "screenshot";
}

return false;
}
</script>
<title>
Recorded Steps
</title>
</head>
<body>
<!-- This is the recorded XML data that was used in generating this page. -->
<xml id="recordeddata">
<script id="myXML" type="text/xml">
<?xml version="1.0" encoding="UTF-8"?>
<Report>
<System MajorVersion="10" MinorVersion="0" ServicePackMajor="0" ServicePackMinor="0" BuildNumber="18362" Sku="101" Platform="2" />
<UserActionData>
<RecordSession SessionCount="1" StartTime="11:51:58 AM" StopTime="11:52:39 AM" ActionCount="11" MissedActionCount="0">
<EachAction ActionNumber="1" Time="11:52:01 AM" Pid="8316" ProgramId="0000f519feec486de87ed73cb92d3cac802400000000" FileId="0000c07130e269ebfeefcb7d893f01498bc96aa5ab24" FileVersion="10.0.18362.1 (WinBuild.160101.0800)" FileDescription="Windows Explorer" FileCompany="Microsoft Corporation" FileName="EXPLORER.EXE" CommandLine="EXPLORER.EXE">
<Description>User left click on "Word - 1 running window (button)"</Description>
<Action>Mouse Left Click</Action>
<CursorCoordsXY>1383,1061</CursorCoordsXY>
<ScreenCoordsXYWH>0,0,1920,1080</ScreenCoordsXYWH>
<UIAStack>
<Level AutomationId="Microsoft.Office.WINWORD.EXE.15" BoundingRectangle="1358,1030,62,50" ControlType="50000" Name="Word - 1 running window" LocalizedControlType="button" />
<Level BoundingRectangle="610,1030,950,50" ClassName="MSTaskListWClass" ControlType="50021" FrameworkId="Win32" Name="Running applications" LocalizedControlType="tool bar" />
<Level BoundingRectangle="610,1030,950,50" ClassName="MSTaskSwWClass" ControlType="50033" FrameworkId="Win32" Name="Running applications" LocalizedControlType="pane" />
<Level AutomationId="40965" BoundingRectangle="608,1030,952,50" ClassName="ReBarWindow32" ControlType="50033" FrameworkId="Win32" LocalizedControlType="pane" />
<Level BoundingRectangle="0,1030,1920,50" ClassName="Shell_TrayWnd" ControlType="50033" FrameworkId="Win32" Name="Taskbar" LocalizedControlType="pane" />
</UIAStack>
<ScreenshotFileName>screenshot0001.JPEG</ScreenshotFileName>
</EachAction>
<EachAction ActionNumber="2" Time="11:52:03 AM" Pid="17892" ProgramId="00062dde378ad4a45da1a2e77a67bb6a717100000000"

之后还有多个……。

我正在尝试使用 Python 检索 EachAction 选项卡中的功能。我无法找到解决方案。有人可以帮我吗?

谢谢

最佳答案

如评论中所述,要获取必要的数据,您可以使用此脚本(对于 txt 变量,我使用了下面所述的 HTML,您可以使用您的 HTML 负载):

soup = BeautifulSoup(txt, 'html.parser')

# locate <script> with XML data
script = soup.select_one('script#myXML')

# parse the XML data
xml_soup = BeautifulSoup(script.contents[0], 'html.parser')

# get data
all_data = []
for each_eaction in xml_soup.select('EachAction'):
all_data.append({'ActionNumber': each_eaction['actionnumber'],
'Time':each_eaction['time'],
'FileDescription':each_eaction['filedescription'],
'Action':each_eaction.find('action').get_text(strip=True)})

# print data:
for line in all_data:
print('{:<30}{:<30}{:<30}{:<30}'.format(*line.values()))

打印:
1                             11:52:01 AM                   Windows Explorer              Mouse Left Click              
2 12:52:01 AM Windows Explorer 1 Mouse Left Click 2
3 10:52:01 AM Windows Explorer 2 Mouse Left Click 3

我使用的 HTML:
txt = '''<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<link href="main.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
function zoomToggle(num)
{
var img = document.getElementById("ss-" + num);

if (img.className == "screenshot") {
img.className = "screenshot-thumb";
}
else {
img.className = "screenshot";
}

return false;
}
</script>
<title>
Recorded Steps
</title>
</head>
<body>
<!-- This is the recorded XML data that was used in generating this page. -->
<xml id="recordeddata">
<script id="myXML" type="text/xml">
<?xml version="1.0" encoding="UTF-8"?>
<Report>
<System MajorVersion="10" MinorVersion="0" ServicePackMajor="0" ServicePackMinor="0" BuildNumber="18362" Sku="101" Platform="2" />
<UserActionData>
<RecordSession SessionCount="1" StartTime="11:51:58 AM" StopTime="11:52:39 AM" ActionCount="11" MissedActionCount="0">
<EachAction ActionNumber="1" Time="11:52:01 AM" Pid="8316" ProgramId="0000f519feec486de87ed73cb92d3cac802400000000" FileId="0000c07130e269ebfeefcb7d893f01498bc96aa5ab24" FileVersion="10.0.18362.1 (WinBuild.160101.0800)" FileDescription="Windows Explorer" FileCompany="Microsoft Corporation" FileName="EXPLORER.EXE" CommandLine="EXPLORER.EXE">
<Description>User left click on "Word - 1 running window (button)"</Description>
<Action>Mouse Left Click</Action>
<CursorCoordsXY>1383,1061</CursorCoordsXY>
<ScreenCoordsXYWH>0,0,1920,1080</ScreenCoordsXYWH>
<UIAStack>
<Level AutomationId="Microsoft.Office.WINWORD.EXE.15" BoundingRectangle="1358,1030,62,50" ControlType="50000" Name="Word - 1 running window" LocalizedControlType="button" />
<Level BoundingRectangle="610,1030,950,50" ClassName="MSTaskListWClass" ControlType="50021" FrameworkId="Win32" Name="Running applications" LocalizedControlType="tool bar" />
<Level BoundingRectangle="610,1030,950,50" ClassName="MSTaskSwWClass" ControlType="50033" FrameworkId="Win32" Name="Running applications" LocalizedControlType="pane" />
<Level AutomationId="40965" BoundingRectangle="608,1030,952,50" ClassName="ReBarWindow32" ControlType="50033" FrameworkId="Win32" LocalizedControlType="pane" />
<Level BoundingRectangle="0,1030,1920,50" ClassName="Shell_TrayWnd" ControlType="50033" FrameworkId="Win32" Name="Taskbar" LocalizedControlType="pane" />
</UIAStack>
<ScreenshotFileName>screenshot0001.JPEG</ScreenshotFileName>
</EachAction>
<EachAction ActionNumber="2" Time="12:52:01 AM" Pid="8316" ProgramId="0000f519feec486de87ed73cb92d3cac802400000000" FileId="0000c07130e269ebfeefcb7d893f01498bc96aa5ab24" FileVersion="10.0.18362.1 (WinBuild.160101.0800)" FileDescription="Windows Explorer 1" FileCompany="Microsoft Corporation" FileName="EXPLORER.EXE" CommandLine="EXPLORER.EXE">
<Description>User left click on "Word - 1 running window (button)"</Description>
<Action>Mouse Left Click 2</Action>
<CursorCoordsXY>1383,1061</CursorCoordsXY>
<ScreenCoordsXYWH>0,0,1920,1080</ScreenCoordsXYWH>
<UIAStack>
<Level AutomationId="Microsoft.Office.WINWORD.EXE.15" BoundingRectangle="1358,1030,62,50" ControlType="50000" Name="Word - 1 running window" LocalizedControlType="button" />
<Level BoundingRectangle="610,1030,950,50" ClassName="MSTaskListWClass" ControlType="50021" FrameworkId="Win32" Name="Running applications" LocalizedControlType="tool bar" />
<Level BoundingRectangle="610,1030,950,50" ClassName="MSTaskSwWClass" ControlType="50033" FrameworkId="Win32" Name="Running applications" LocalizedControlType="pane" />
<Level AutomationId="40965" BoundingRectangle="608,1030,952,50" ClassName="ReBarWindow32" ControlType="50033" FrameworkId="Win32" LocalizedControlType="pane" />
<Level BoundingRectangle="0,1030,1920,50" ClassName="Shell_TrayWnd" ControlType="50033" FrameworkId="Win32" Name="Taskbar" LocalizedControlType="pane" />
</UIAStack>
<ScreenshotFileName>screenshot0001.JPEG</ScreenshotFileName>
</EachAction>
<EachAction ActionNumber="3" Time="10:52:01 AM" Pid="8316" ProgramId="0000f519feec486de87ed73cb92d3cac802400000000" FileId="0000c07130e269ebfeefcb7d893f01498bc96aa5ab24" FileVersion="10.0.18362.1 (WinBuild.160101.0800)" FileDescription="Windows Explorer 2" FileCompany="Microsoft Corporation" FileName="EXPLORER.EXE" CommandLine="EXPLORER.EXE">
<Description>User left click on "Word - 1 running window (button)"</Description>
<Action>Mouse Left Click 3</Action>
<CursorCoordsXY>1383,1061</CursorCoordsXY>
<ScreenCoordsXYWH>0,0,1920,1080</ScreenCoordsXYWH>
<UIAStack>
<Level AutomationId="Microsoft.Office.WINWORD.EXE.15" BoundingRectangle="1358,1030,62,50" ControlType="50000" Name="Word - 1 running window" LocalizedControlType="button" />
<Level BoundingRectangle="610,1030,950,50" ClassName="MSTaskListWClass" ControlType="50021" FrameworkId="Win32" Name="Running applications" LocalizedControlType="tool bar" />
<Level BoundingRectangle="610,1030,950,50" ClassName="MSTaskSwWClass" ControlType="50033" FrameworkId="Win32" Name="Running applications" LocalizedControlType="pane" />
<Level AutomationId="40965" BoundingRectangle="608,1030,952,50" ClassName="ReBarWindow32" ControlType="50033" FrameworkId="Win32" LocalizedControlType="pane" />
<Level BoundingRectangle="0,1030,1920,50" ClassName="Shell_TrayWnd" ControlType="50033" FrameworkId="Win32" Name="Taskbar" LocalizedControlType="pane" />
</UIAStack>
<ScreenshotFileName>screenshot0001.JPEG</ScreenshotFileName>
</EachAction>
</UserActionData>
</Report>
</script>
</body>
</html>'''

关于python - 读取 MHTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62158595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com