2015-06-11 25 views
1

我真的被陷在我試圖做的事情。 我想打一個非常簡單的腳本,以顯示谷歌瀏覽器的歷史記錄。 當我使用下面的代碼行:蟒蛇3.4谷歌瀏覽器的歷史

f = open('C:\\Users\\joey\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\History', 'rb') 
data = f.read() 
f.close() 

我得到一個輸出,我只西港島線顯示部分,因爲它西港島線太長,否則。

x00\x00\x00\x00\x00\x81#\x9cU\n\x00\x81yC\t\x08\x06\x08\x08https://www.google.nl/search?q=de+zigodoom&oq=de+zigodoom&aqs=chrome..69i57.1507j0j7&sourceid=chrome&es_sm=93&ie=UTF-8de zigodoom 

如何顯示網站而不是所有的x00/x000輸出。我怎樣才能在不同的行上顯示每個網站。

for d in data: 
print(data) 

Wil這樣容易的循環工作?

+0

在文件頂部顯示[SQLite](https://docs.python.org/2/library/sqlite3.html)格式。 –

+0

可能重複:http://stackoverflow.com/questions/4643142/regex-to-test-if-string-begins-with-http-or-https – sgp

+0

長話短說,歷史文件是** **不只是一個平面文本的網站列表。此外,您的'for'循環是沒有意義的 - 這是不正確的縮進你爲什麼要打印你遍歷對象* *以上的循環中? – jonrsharpe

回答

2

谷歌Chrome瀏覽器的歷史記錄存儲在一個數據庫SQLite。自從Python 2.5以來,Python已經通過sqlite3支持這種開箱即用的方式。

import sqlite3 
con = sqlite3.connect('C:\\Users\\joey\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\History') 
cursor = con.cursor() 
cursor.execute("SELECT url FROM urls") 
urls = cursor.fetchall() 
print('\n'.join(urls)) 
+1

你好!非常感謝您的幫助。 我得到下一個錯誤:sqlite3.OperationalError:數據庫被鎖定 – joey

+0

這是因爲Chrome正在使用它。 –

+0

關閉Chrome,仍然給我同樣的錯誤 –