Skip to content
Related Articles

Related Articles

Improve Article

Get Browser History using Python in Ubuntu

  • Last Updated : 01 Jun, 2021

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 
 

  1. Import os module and sqlite3 module.
  2. os.path.expanduser() method is used to expand an initial path component ~ or path to user’s home directory.
  3. 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.
  4. Connect method of sqlite3 then connects with the database.
  5. Once connection is successful, it creates the object of cursor inorder to access the tables
  6. Execute method of cursor objects runs the query on sqlite3 database and records are fetched using fetchall method and stored in results
  7. for loop prints the records fetched from the database
  8. 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 
 

  1. Import sqlite3 module.
  2. Connect method of sqlite3 then connects with the database. Note that path “home/admin1” differs according to system’s root path
  3. Once connection is successful, it creates the object of cursor inorder to access the tables
  4. Execute method of cursor objects runs the query on sqlite3 database and records are fetched using fetchall method and stored in results
  5. for loop prints the records fetched from the database
  6. 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :