【发布时间】:2021-09-29 13:28:31
【问题描述】:
我无法在 Python 中识别正确的元素。我真正想看到的是 recently-used.xbel 中最新访问的文件。因此,我想遍历每个文件以找到具有最新 modified 或最新 visited 的文件 这就是 XML 文件的样子。
<?xml version="1.0" encoding="UTF-8"?>
<xbel version="1.0"
xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"
xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"
>
<bookmark href="file:///tmp/google-chrome-stable_current_amd64.deb" added="2021-09-14T12:09:05Z" modified="2021-09-14T12:09:05Z" visited="2021-09-15T09:12:13Z">
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type type="application/vnd.debian.binary-package"/>
<bookmark:applications>
<bookmark:application name="Firefox" exec="'firefox %u'" modified="2021-09-14T12:09:05Z" count="1"/>
</bookmark:applications>
</metadata>
</info>
</bookmark>
<bookmark href="file:///home/test/Git/testprog" added="2021-09-15T09:12:13Z" modified="2021-09-15T09:12:13Z" visited="2021-09-15T09:12:13Z">
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type type="inode/directory"/>
<bookmark:applications>
<bookmark:application name="code" exec="'code %u'" modified="2021-09-15T09:12:13Z" count="1"/>
</bookmark:applications>
</metadata>
</info>
</bookmark>
<bookmark href="file:///home/test/.local/share/recently-used.xbel" added="2021-09-15T09:51:57Z" modified="2021-09-15T09:51:57Z" visited="2021-09-15T09:51:57Z">
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type type="application/x-xbel"/>
<bookmark:applications>
<bookmark:application name="code" exec="'code %u'" modified="2021-09-15T09:51:57Z" count="1"/>
</bookmark:applications>
</metadata>
</info>
</bookmark>
<bookmark href="file:///tmp/slack-desktop-4.19.2-amd64.deb" added="2021-09-15T11:45:49Z" modified="2021-09-15T11:45:49Z" visited="2021-09-16T13:26:26Z">
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type type="application/vnd.debian.binary-package"/>
<bookmark:applications>
<bookmark:application name="Firefox" exec="'firefox %u'" modified="2021-09-15T11:45:49Z" count="1"/>
</bookmark:applications>
</metadata>
</info>
</bookmark>
<bookmark href="file:///home/test/Downloads/google-chrome-stable_current_amd64.deb" added="2021-09-15T11:52:39Z" modified="2021-09-15T11:52:39Z" visited="2021-09-16T13:26:26Z">
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type type="application/vnd.debian.binary-package"/>
<bookmark:applications>
<bookmark:application name="Firefox" exec="'firefox %u'" modified="2021-09-15T11:52:39Z" count="1"/>
</bookmark:applications>
</metadata>
</info>
</bookmark>
<bookmark href="file:///home/test/Documents/libretest" added="2021-09-15T11:58:53Z" modified="2021-09-15T11:58:53Z" visited="2021-09-16T13:26:26Z">
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type type="application/octet-stream"/>
<bookmark:applications>
<bookmark:application name="LibreOffice 6.4" exec="'soffice %u'" modified="2021-09-15T11:58:53Z" count="1"/>
</bookmark:applications>
</metadata>
</info>
</bookmark>
<bookmark href="file:///home/test/Documents/libretest.odt" added="2021-09-15T11:58:53Z" modified="2021-09-15T15:42:04Z" visited="2021-09-16T13:26:26Z">
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type type="application/vnd.oasis.opendocument.text"/>
<bookmark:applications>
<bookmark:application name="LibreOffice 6.4" exec="'soffice %u'" modified="2021-09-15T15:42:04Z" count="12"/>
</bookmark:applications>
</metadata>
</info>
</bookmark>
<bookmark href="file:///home/test/Git/node-socket" added="2021-09-16T13:26:25Z" modified="2021-09-16T13:26:49Z" visited="2021-09-16T13:26:26Z">
<info>
<metadata owner="http://freedesktop.org">
<mime:mime-type type="inode/directory"/>
<bookmark:applications>
<bookmark:application name="code" exec="'code %u'" modified="2021-09-16T13:26:49Z" count="2"/>
</bookmark:applications>
</metadata>
</info>
</bookmark>
</xbel>
在我的代码中,我试图访问bookmark:applications,但没有成功。
home = str(Path.home())
root = ET.parse(home + '/.local/share/recently-used.xbel').getroot()
print(root)
print('lower')
for bookmark in root.iter('bookmark'):
print(bookmark)
for applications in bookmark.find('applications'):
print(applications)
访问bookmark:applications 并找到上次访问的正确方法是什么?
【问题讨论】:
-
尝试使用
xmltodict -
bookmark:applications元素绑定到http://www.freedesktop.org/standards/desktop-bookmarks命名空间(通过xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks")。见docs.python.org/3/library/… -
如果您只需要最后修改的书签信息,那么您可以使用书签标签的修改属性来做到这一点,因为书签的修改属性和书签的修改属性:应用程序都具有相同的值。