Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python跟踪在web浏览器中打开的网页?_Python_Python 3.x_Browser - Fatal编程技术网

如何使用Python跟踪在web浏览器中打开的网页?

如何使用Python跟踪在web浏览器中打开的网页?,python,python-3.x,browser,Python,Python 3.x,Browser,我想写一个Python脚本,它可以跟踪在我的webbrowser(Mozilla Firefox 23)中打开了哪些网页。我不知道从哪里开始。Python的标准webbrowser模块允许打开网页,但标准文档没有任何关于与网页交互的内容 那么,我是否需要为我的浏览器编写一个插件,该插件可以将数据发送到我的Python脚本,以防标准库中缺少功能 我已经研究了一些相关的问题,比如,但它们都是关于使用mechanize和/或selenium在Python中模拟web浏览器的。我不想那样做。我想使用标准

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

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

我已经研究了一些相关的问题,比如,但它们都是关于使用mechanize和/或selenium在Python中模拟web浏览器的。我不想那样做。我想使用标准Python库从webbrowser中获取数据

编辑


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

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

如果我理解得很好,您希望检查访问页面的历史记录。问题是它与HTML、http协议或web服务都没有直接关系。历史记录(按下Ctrl-H键时可以在Firefox中看到)是Firefox中实现的工具,因此,它肯定依赖于实现。不可能有能够提取信息的标准库

至于HTTP协议和HTML中的页面内容,没有什么比与页面内容交互更好的了。该协议使用GET with URL作为参数,web服务器发回包含一些元信息的文本体。调用方(浏览器)可以对返回的数据执行任何操作。浏览器使用标记的文本,并将其解释为可读的文档,其中的部分呈现得尽可能好。交互(单击a 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

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


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

建设性批评:发表评论解释否决票不会有什么坏处。它有助于避免将来出现不好的问题。这对于任何投票结束它的人来说都是太广泛了。我特别写了“我想使用标准Python库从我的webbrowser获取数据”。我将其标记为Python3,Python的理念是应该有一种方法来做事情,Python3处理Python2中的许多冗余。那怎么太宽了?请解释一下。我想听听解释。为了让问题更清楚,我想跟踪firefox中当前打开的网页。我不认为这会改变你们的答案,但以防万一,是吗?Firefox会在某处存储打开页面的URL。您可能已经注意到FF能够在碰撞后恢复它们。您还可以配置FF,使其打开关闭FF时可见的页面。我想获取URL列表是很容易的。如果您想更新答案:选项卡当前存储在名为
recovery.js
js文件中,该文件每15秒更新一次,路径最初是相同的,但在末尾添加了一个文件夹:“\sessionstore backups”。你剩下的答案仍然有效。@erasmortg:谢谢你提供的信息。我刚刚发布了更新说明,并指出了您的评论。玩得开心;)你的英语不错,谢谢你的回答。