analitics

Pages

Sunday, March 17, 2019

Get bookmarks from your Firefox browser database.

This simple example tutorial is about reading the bookmarks from firefox database.
The database is an SQLite database.
You need to create a python file named: firefox_bookmarks.py.
Change your windows account on the bookmarks_path.
The script is simple to understand and comes with two functions: execute_query and get_bookmarks.
Follow the commented source code to understand how it's working the python script:
import os
import sqlite3

# execute a query on sqlite cursor
def execute_query(cursor, query):
    try:
        cursor.execute(query)
    except Exception as error:
        print(str(error) + "\n " + query)
# get bookmarks from firefox sqlite database file and print all
def get_bookmarks(cursor):
    bookmarks_query = """select url, moz_places.title, rev_host, frecency,
    last_visit_date from moz_places  join  \
    moz_bookmarks on moz_bookmarks.fk=moz_places.id where visit_count>0
    and moz_places.url  like 'http%'
    order by dateAdded desc;"""
    execute_query(cursor, bookmarks_query)
    for row in cursor:
        link = row[0]
        title = row[1]
        print(link,title)
# set the path of firefox folder with databases
bookmarks_path = "C:/Users/YOUR_WINDOWS_ACCOUNT/AppData/Roaming/Mozilla/Firefox/Profiles/"
# get firefox profile
profiles = [i for i in os.listdir(bookmarks_path) if i.endswith('.default')]
# get sqlite database of firefox bookmarks
sqlite_path = bookmarks_path+ profiles[0]+'/places.sqlite'
#
if os.path.exists(sqlite_path):
    firefox_connection = sqlite3.connect(sqlite_path)
cursor = firefox_connection.cursor()
get_bookmarks(cursor)
cursor.close()
The result of running the script comes with my bookmarks of Firefox:
C:\Python364>python.exe firefox_bookmarks.py
https://twitter.com/ Twitter. It's what's happening.