【问题标题】:How to keep track of webpages opened in web-browser using Python? [closed]如何使用 Python 跟踪在网络浏览器中打开的网页? [关闭]
【发布时间】:2013-08-28 16:09:44
【问题描述】:

我想编写一个 Python 脚本,它可以跟踪在我的网络浏览器(Mozilla Firefox 23)中打开了哪些网页。我不知道从哪里开始。 Python 的标准webbrowser 模块允许打开网页,但标准文档没有任何关于与网页交互的内容。

那么我是否需要为我的浏览器编写一个插件,它可以将数据发送到我的 Python 脚本,因为我是否缺少标准库中的功能?

我查看了一些相关问题,例如this,但它们都是关于使用 mechanize 和/或 selenium 在 Python 中模拟网络浏览器。我不想那样做。我想使用标准 Python 库从我的网络浏览器中获取数据。

编辑

为了更清楚地说明问题,我想跟踪当前在 Firefox 中打开的网页。

【问题讨论】:

  • 建设性批评:发表评论解释否决票永远不会受到伤害。它可以帮助避免将来出现不好的问题。
  • 这适用于任何投票关闭它的人,因为它过于宽泛。我专门写过“我想使用 标准 Python 库从我的网络浏览器中获取数据。”我将其标记为 Python3,Python 的理念是应该有一种方法来做事,而 Python3 处理了 Python2 中的许多冗余。那怎么太宽泛了?请解释。我想听听解释。

标签: python python-3.x browser


【解决方案1】:

这个答案可能有点模糊——那是因为问题不是很具体。

如果我理解得很好,您想检查访问页面的历史。问题在于它与 HTML、http 协议和 Web 服务没有直接关系。历史记录(您可以在 Firefox 中按 Ctrl-H 观察)是 Firefox 中实现的工具,因此,它绝对依赖于实现。不可能有能够提取信息的标准库。

关于 HTTP 协议和 HTML 中的页面内容,与页面内容没有任何类似的交互。该协议使用带有 URL 作为参数的 GET,Web 服务器发回带有一些元信息的文本正文。调用者(浏览器)可以对返回的数据做任何事情。浏览器使用标记的文本并将其解释为可读的文档,其中的部分尽可能好地呈现。交互(点击href)由浏览器实现。它会导致 http 协议的其他 GET 命令。

要回答您的问题,您需要了解 Mozilla Firefox 23 如何存储历史记录。您很可能可以在内部 SQLite 数据库的某个地方找到它。

2015-08-24 更新:请参阅 erasmortg 对在 Firefox 中放置信息的更改的评论。 (下面的文字比这个更老。)

更新:打开的标签列表与用户绑定。正如您可能希望它用于 Windows,您应该首先获得类似 c:\Users\myname.mydomain\AppData\Roaming\Mozilla\Firefox\Profiles\yoodw5zk.default-1375107931124\sessionstore.js 的路径。配置文件名称可能应该从c:\Users\myname.mydomain\AppData\Roaming\Mozilla\Firefox\profiles.ini 中提取。我刚刚复制了 sessionstore.js 以尝试获取数据。正如它所说的javascript,我确实使用了标准的json 模块来解析它。你基本上得到了字典。带有'windows' 键的项目之一包含另一个字典,而其'tabs' 又包含有关选项卡的信息。

将您的sessionstore.js 复制到工作目录并在那里执行以下脚本:

#!python3

import json

with open('sessionstore.js', encoding='utf-8') as f:
    content = json.load(f)

# The loaded content is a dictionary. List the keys first (console).
for k in content:
    print(k)

# Now list the content bound to the keys. As the console may not be capable
# to display all characters, write it to the file.
with open('out.txt', 'w', encoding='utf-8') as f:

    # Write the overview of the content.
    for k, v in content.items():
        # Write the key and the type of the value.
        f.write('\n\n{}:  {}\n'.format(k, type(v)))

        # The value could be of a list type, or just one item.
        if isinstance(v, list):
            for e in v:
                f.write('\t{}\n'.format(e))
        else:
            f.write('\t{}\n'.format(v))

    # Write the content of the tabs in each windows.
    f.write('\n\n=======================================================\n\n')
    windows = content['windows']
    for n, w in enumerate(windows, 1):  # the enumerate is used just for numbering the windows
        f.write('\n\tWindow {}:\n'.format(n))
        tabs = w['tabs']
        for tab in tabs:
            # The tab is a dictionary. Display only 'title' and 'url' from 
            # 'entries' subdictionary.
            e = tab['entries'][0]
            f.write('\t\t{}\n\t\t{}\n\n'.format(e['url'], e['title']))

结果既显示在控制台上(几行),又写入工作目录中的out.txt 文件。 out.txt(在文件末尾)在我的例子中包含类似的内容:

Window 1:
    http://www.cyrilmottier.com/
    Cyril Mottier

    http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
    Fragments | Android Developers

    http://developer.android.com/guide/components/index.html
    App Components | Android Developers

    http://www.youtube.com/watch?v=ONaD1mB8r-A
    ▶ Introducing RoboSpice: A Robust Asynchronous Networking Library for Android - YouTube

    http://www.youtube.com/watch?v=5a91dBLX8Qc
    Rocking the Gradle with Hans Dockter - YouTube

    http://stackoverflow.com/questions/18439564/how-to-keep-track-of-webpages-opened-in-web-browser-using-python
    How to keep track of webpages opened in web-browser using Python? - Stack Overflow

    https://www.google.cz/search?q=Mozilla+firefox+list+of+open+tabs&ie=utf-8&oe=utf-8&rls=org.mozilla:cs:official&client=firefox-a&gws_rd=cr
    Mozilla firefox list of open tabs - Hledat Googlem

    https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/tutorials/list-open-tabs.html
    List Open Tabs - Add-on SDK Documentation

    https://support.mozilla.org/cs/questions/926077
    list all tabs button not showing | Fórum podpory Firefoxu | Podpora Mozilly

    https://support.mozilla.org/cs/kb/scroll-through-your-tabs-quickly
    Scroll through your tabs quickly | Nápověda k Firefox

【讨论】:

  • 只是为了更清楚地说明问题,我想跟踪当前在 Firefox 中打开的网页。我认为这不会改变你的答案,但以防万一,是吗?
  • Firefox 将打开页面的 URL 存储在某处。您可能已经观察到 FF 能够在崩溃后恢复它们。您还可以配置 FF 以便它打开在关闭 FF 时可见的页面。我想得到那个 URL 列表会很容易。
  • 如果您想更新答案:选项卡当前存储在名为 recovery.jsjs 文件中,每 15 秒更新一次,see here for details,路径最初相同,但添加了一个最后的文件夹:`\sessionstore-backups`。您的其余答案仍然有效。
  • @erasmortg:感谢您提供的信息。我刚刚发布了更新说明并指出了您的评论。玩得开心;)
【解决方案2】:

您希望通过 Python 跟踪在 FF 中打开的网页。那么,为什么不用 Python 编写一个 Web 代理并配置 FireFox 以使用该 Web 代理。

之后,您可以通过正则表达式过滤从 Firefox 发出的所有 HTTP 请求,并将它们存储在文件或数据库中。

【讨论】:

  • 你的英语不错,谢谢你的回答。
猜你喜欢
  • 2012-06-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 2010-10-10
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多