Get Browser History using Python in Ubuntu - GeeksforGeeks

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. 
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 codeWorking 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 tablesExecute method of cursor objects runs the query on sqlite3 database and records are fetched using fetchall method and stored in resultsfor loop prints the records fetched from the databaseIt’s important to close the cursor, when it’s task is done as it releases all the locks on sqlite3 database.
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.
 
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 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 codeWorking 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 pathOnce connection is successful, it creates the object of cursor inorder to access the tablesExecute method of cursor objects runs the query on sqlite3 database and records are fetched using fetchall method and stored in resultsfor loop prints the records fetched from the databaseIt’s important to close the cursor when it’s task is done as it releases all the locks on sqlite3 database.
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.
 
surinderdawra388
Python-projects
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python