如何使用 Python 跟踪在网络浏览器中打开的网页? [关闭]

2024-02-01

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

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

我看过一些相关的问题,例如this https://stackoverflow.com/questions/1292817/how-to-automate-browsing-using-python但它们都是关于使用 mechanize 和/或 selenium 在 Python 中模拟网络浏览器。我不想那样做。我想使用标准 Python 库从我的网络浏览器获取数据。

EDIT

只是为了让问题更加清晰,我想跟踪当前在 Firefox 中打开的网页。


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

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

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

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

2015年8月24日更新:请参阅erasmortg关于在Firefox中放置信息的变化的评论。 (下面的文字比本文更旧。)

Update:打开的选项卡列表与用户绑定。由于您可能希望在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Python 跟踪在网络浏览器中打开的网页? [关闭] 的相关文章

随机推荐