3

I am really getting stuck on the thing I am trying to make. I want to make a really simple script to display the history of Google Chrome. When I use the following lines of code:

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

I get the next output, I wil only show a part because it wil be too long otherwise.

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

How can I just display the websites and not all the x00/x000 output. And how can I show each website on a different row.

for d in data:
print(data)

Wil something like this easy for loop work?

Colin 't Hart
  • 7,372
  • 3
  • 28
  • 51
joey
  • 241
  • 1
  • 4
  • 16
  • At the top of the file it says [SQLite](https://docs.python.org/2/library/sqlite3.html) format. – Peter Wood Jun 11 '15 at 10:15
  • Possible duplicate : http://stackoverflow.com/questions/4643142/regex-to-test-if-string-begins-with-http-or-https – sgp Jun 11 '15 at 10:15
  • Long story short, the history file is **not** just a flat-text list of websites. Also your `for` loop makes no sense - it's incorrectly indented and why would you print the object you're iterating *over* inside the loop?! – jonrsharpe Jun 11 '15 at 10:23

1 Answers1

4

Google Chrome's history is stored in an SQLite database. Python has supported this out of-the-box through sqlite3 since Python 2.5.

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))
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • 1
    Hello! Thanks a lot for helping me. I am getting the next error: sqlite3.OperationalError: database is locked – joey Jun 11 '15 at 10:28
  • That's because Chrome is using it. – Peter Wood Jun 11 '15 at 10:28
  • @whackamadoodle3000 sometimes Chrome doesn't close and keeps background tasks running. You might be able to make a copy without having write permissions. – Peter Wood Aug 04 '17 at 15:33
  • @PeterWood how do you make a copy? – Dianne Dec 16 '21 at 20:22
  • @Dianne the same way you would copy any file. If you're using python you could use [`shutil`](https://docs.python.org/3/library/shutil.html). I searched google for "copy a file python" and this was the first result: https://stackoverflow.com/questions/123198/how-to-copy-files – Peter Wood Dec 19 '21 at 20:53