Get Browser History using Python in Ubuntu
Last Updated :
28 Apr, 2025
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 os
import sqlite3
# Build Data path
data_path = os.path.expanduser('~')+"/.mozilla/firefox/ri27ye3b.default"
history_db = os.path.join(data_path, 'places.sqlite')
# Make connection with sqlite3 database
c = sqlite3.connect(history_db)
# Create cursor object to execute query
cursor = 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 result
results = cursor.fetchall()
for url, count in results:
print(url)
# Close the cursor
cursor.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 sqlite3
con = sqlite3.connect('/home/admin1/.config/google-chrome/Default/History')
c = con.cursor()
# Change this to your preferred query
c.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.
Similar Reads
Delete Google Browser History using Python In this article, you will learn to write a Python program which will take input from the user as a keyword like Facebook, amazon, geeksforgeeks, Flipkart, youtube, etc. and then search your google chrome browser history for that keyword and if the keyword is found in any of the URL then it will dele
2 min read
How to get the current username in Python When building interacting Python programs you might need to get the current username for features like personalizing user experience, providing user-specific resources, Implementing robust logging and debugging, strengthening security, etc. We will use different Python modules and functions like os.
4 min read
Python Version History Python, one of the most popular programming languages today, has a rich history of development and evolution. From its inception in the late 1980s to its current status as a versatile and powerful language, Python's version history reflects the language's adaptability and the community's dedication
5 min read
Get current date using Python Prerequisite: DateTime moduleIn Python, Date and Time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The DateTime module provides some functions
3 min read
Get Current Timestamp Using Python A timestamp is a sequence of characters that represents the date and time at which a particular event occurred, often accurate to fractions of a second. Timestamps are essential in logging events, tracking files, and handling date-time data in various applications. There are 3 different ways to get
2 min read
Get Current directory in Python In this article, we will cover How to Get and Change the Working Directory in Python. While working with file handling you might have noticed that files are referenced only by their names, e.g. 'GFG.txt' and if the file is not located in the directory of the script, Python raises an error. The conce
3 min read