从 Linux 中打开的窗口获取内容

发布于 2024-11-06 08:43:26 字数 224 浏览 5 评论 0原文

我想收集数据并最终从 Linux 中打开的窗口解析它。

一个例子 - 假设终端窗口打开。我需要检索该窗口上显示的所有数据。检索后,我会对其进行解析以获取输入的特定命令。

那么可以这样做吗?如果是这样,怎么办?我更喜欢使用 python 来编码这整个事情。

我猜测,首先我必须获得打开窗口的某种 ID,然后使用某种库从我获得 ID 的窗口中获取内容。

请帮忙。我是个新手。

I want to collect data and parse it eventually from an open window in linux.

An example- Suppose a terminal window is open. I need to retrieve all the data that appears on that window. After retrieval, I would parse it to get specific commands entered.

So is it possible to do that? If so, how? I would prefer to use python to code this entire thing.

I am making a guess that first I would have to get some sort of ID for the open window and then use some kind of library to get the content from the window whose ID I have got.

Please help. I am quite a newbie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染火枫林 2024-11-13 08:43:26

您可以(ab)使用工具包库中存在的辅助技术支持(用于屏幕阅读器等)。它是否有效取决于特定的工具包——Gtk 和 Qt 有这种支持,但其他的(如 Tk、Fltk 等)可能支持也可能不支持。

Linux 桌面测试项目 是一个 python 工具包,用于滥用这些接口来测试 GUI 应用程序,因此您可以使用它或看看它是如何工作的并做类似的事情。

You can (ab)use the assistive technologies support (for screen readers and such) that exist in the toolkit libraries. Whether it will work is toolkit specific—Gtk and Qt have this support, but others (like Tk, Fltk, etc.) may or may not.

The Linux Desktop Testing Project is a python toolkit for abusing these interfaces for testing GUI applications, so you can either use it or look how it works and do similar thing.

橙味迷妹 2024-11-13 08:43:26

我认为正确的答案可能是“有一些困难”。本质上,窗口的内容是位图。该位图由大量基元绘制(包括“使用该编码和特定字体显示该八位字节字符串”),但窗口内容仍然“只是像素”。

就这些事情而言,获得“仅像素”非常简单。您打开一个到 X 服务器的会话并说“给我窗口 W 的内容”,然后它就会将其移交。

不幸的是,用它做一些有用的事情是完全不同的事情,因为您可能必须(本质上)对位图进行 OCR 以获得您想要的内容。

如果您决定采用该路线,请查看 xwd 的源代码,因为本质上就是这样。

I think the correct answer may be "with some difficulty". Essentially, the contents of a window is a bitmap. This bitmap is drawn on by a whole slew of primitives (including "display this octet-string, using that encoding and a specific font"), but the window contents is still "just pixels".

Getting the "just pixels" is pretty straight-forward, as these things go. You open a session to the X server and say "given me the contents of window W" and it hands it over.

Doing something useful with it is, unfortunately, a completely different matter, as you'd potentially have to (essentially) OCR the bitmap for what you want.

If you decide to take that route, have a look at the source of xwd, as that does, essentially, that.

赠佳期 2024-11-13 08:43:26

您对终端的执行有某种控制吗?在这种情况下,您可以在终端会话中使用 script 命令将所有交互记录到文件中,然后读取并解析该文件。

$ script myfile
Script started, file is myfile
$ ls
...
$ exit
Script done, file is myfile
$ parse_file.py myfile

如果终端在屏幕内运行,您还有其他选项。 Screen 内置了日志记录,screen -X 将命令发送到正在运行的 screen 会话 (man屏幕)。

Do you have some sort of control over the execution of the terminal? In that case, you can use the script command in the terminal session to log all interaction to a file and then read and parse the file.

$ script myfile
Script started, file is myfile
$ ls
...
$ exit
Script done, file is myfile
$ parse_file.py myfile

If the terminal is running inside of screen, you have other options as well. Screen has logging built in, screen -X sends commands to a running screen session (man screen).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文