Get Browser History using Python in Ubuntu
In order to get the browser history of chrome and Mozilla Firefox browser os module and sqlite3 modules are used. The Chrome and Firefox history data are stored in SQLite database. So SQLite Python package is needed to extract the data from the browser history.
Get History from Firefox
The Firefox browser stores all details in .mozilla/firefox folder. The history file has extension .default. To get the history from the firefox browser follow the below-given steps. Get the name of the default file storing browser history from the terminal as mentioned below

Note : Name of file storing history differs in the system but extension remains same(.default)
Example
Python
import osimport sqlite3# Build Data pathdata_path = os.path.expanduser('~')+"/.mozilla/firefox/ri27ye3b.default"history_db = os.path.join(data_path, 'places.sqlite')# Make connection with sqlite3 databasec = sqlite3.connect(history_db)# Create cursor object to execute querycursor = c.cursor()select_statement = "select moz_places.url, moz_places.visit_count from moz_places;"cursor.execute(select_statement)# Fetch the result and Prints the resultresults = cursor.fetchall()for url, count in results: print(url) # Close the cursorcursor.close() |
Note: Your browser should be closed when you execute the above-given python code. When the browser is opened, it acquires lock on the database so it will not allow the access to python code
Working of code is as mentioned below
- Import os module and sqlite3 module.
- os.path.expanduser() method is used to expand an initial path component ~ or path to user’s home directory.
- os.path.join() method joins one or more path components with exactly one directory separator (‘/’) following each non-empty part except the last path component to get the final path.
- Connect method of sqlite3 then connects with the database.
- Once connection is successful, it creates the object of cursor inorder to access the tables
- Execute method of cursor objects runs the query on sqlite3 database and records are fetched using fetchall method and stored in results
- for loop prints the records fetched from the database
- It’s important to close the cursor, when it’s task is done as it releases all the locks on sqlite3 database.
Get History from Chrome
The Chrome browser stores all details in .config/google-chrome/Default folder. Here History is name of database where browser history is stored.
To get the history from chrome browser follow the below given steps
To check the presence of History db follow the steps given in an image below.

Example
Python
import sqlite3con = sqlite3.connect('/home/admin1/.config/google-chrome/Default/History')c = con.cursor()# Change this to your preferred queryc.execute("select url, title, visit_count, last_visit_time from urls")results = c.fetchall()for r in results: print(r) c.close() |
Note: Your browser should be closed when you execute the above-given Python code. When the browser is opened, it acquires lock on the database so it will not allow the access to Python code
Working of code is as mentioned below
- Import sqlite3 module.
- Connect method of sqlite3 then connects with the database. Note that path “home/admin1” differs according to system’s root path
- Once connection is successful, it creates the object of cursor inorder to access the tables
- Execute method of cursor objects runs the query on sqlite3 database and records are fetched using fetchall method and stored in results
- for loop prints the records fetched from the database
- It’s important to close the cursor when it’s task is done as it releases all the locks on sqlite3 database.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course



