Ase*_*sal -1 python browser python-3.x
我想写一个Python脚本,可以跟踪我的webbrowser(Mozilla Firefox 23)中打开了哪些网页.我不知道从哪里开始.webbrowser
Python 的标准模块允许打开网页,但标准文档没有关于与网页交互的任何内容.
所以我需要为我的浏览器编写一个插件,它可以将数据发送到我的Python脚本,因为我缺少标准库中的功能吗?
我已经看过一些像这样的相关问题,但它们都是关于使用mechanize和/或selenium在Python中模拟Web浏览器.我不想那样做.我想从我的webbrowser中获取使用标准Python库的数据.
编辑
只是为了增加问题的清晰度,我想跟踪当前在Firefox中打开的网页.
这个答案可能有点模糊 - 这是因为问题并不是非常具体.
如果我理解得很好,你想要检查被访问页面的历史.问题是它与HTML,http协议和Web服务没有直接关系.历史记录(您可以在按下Ctrl-H时在Firefox中观察到)是在Firefox中实现的工具,因此,它绝对是依赖于实现的.没有能够提取信息的标准库.
至于HTTP协议和HTML中页面的内容,没有什么比与页面内容的交互更好了.该协议使用GET和URL作为参数,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']))
Run Code Online (Sandbox Code Playgroud)
结果显示在控制台上(几行),并写入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
Run Code Online (Sandbox Code Playgroud)