2013-08-26 83 views
-2

我想寫一個Python腳本,可以跟蹤哪些網頁已經在我的瀏覽器(Mozilla Firefox 23)中打開過。我不知道從哪裏開始。 Python的標準webbrowser模塊允許打開網頁,但標準文檔沒有任何關於與網頁交互的內容。如何跟蹤使用Python在Web瀏覽器中打開的網頁?

那麼,我需要爲我的瀏覽器編寫一個插件,它可以將數據發送到我的Python腳本,我是否缺少標準庫的功能?

我已經看過一些相關的問題,如this,但它們都是關於使用機械化和/或硒模擬Python中的Web瀏覽器。我不想這樣做。我想從使用標準Python庫的webbrowser獲取數據。

編輯

只是一些更清晰添加到這個問題,我想跟蹤當前網頁在Firefox開放的。

+4

建設性批評:發表評論解釋downvote永遠不會傷害。它可以幫助避免將來出現不好的問題。 –

+4

**這是任何人投票結束太寬**。我專門寫了「我想使用**標準Python庫**從我的瀏覽器獲取數據。」我將它標記爲Python3,而Python有一個哲學理念:**應該有一種方法來做事**,而Python3在Python2中處理了許多冗餘。那麼它如何過於寬泛?請解釋。我想聽聽解釋。 –

回答

4

這個答案可能有點模糊 - 這是因爲問題不是非常具體。

如果我理解得很好,您想查看訪問頁面的歷史。問題在於它不直接與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 
+0

只是爲了增加一些更清晰的問題,我想跟蹤當前在Firefox中打開的網頁。我不認爲這會改變你的答案,但以防萬一,是嗎? –

+0

Firefox在某處存儲打開頁面的URL。您可能已經觀察到FF在碰撞後能夠恢復它們。您也可以配置FF,以便打開在關閉FF時可見的頁面。我想這將是很容易得到的URL列表。 – pepr

+1

如果您想更新您的答案:標籤目前存儲在每15秒更新一次的名爲'recovery.js'的js文件中,[詳情請參閱此處](https://dutherenverseauborddelatable.wordpress.com/2014/ 06/26/firefox-the-browser-that-has-your-backup /),路徑最初是相同的,但在最後有一個添加的文件夾:'\ sessionstore-backups \'。你的答案的其餘部分仍然存在。 – erasmortg

1

你要跟蹤的網頁,通過Python的在FF打開的。那麼,爲什麼不用Python編寫Web代理並配置FireFox來使用該Web代理。

之後,您可以通過正則表達式過濾所有從Firefox發出的HTTP請求,並將它們存儲在文件或數據庫中。

+0

你的英語不錯,謝謝你的回答。 –

相關問題