Python practice: copy Firefox bookmarks from bookmarks.html to bookmarks.txt

#! usr/bin/env python3
# by lorenzo - 05/10/2017
# copy Firefox bookmarks from bookmarks.html to bookmarks.txt file
# in the same folder of bookmarks.html

import bs4, os, sys

print("Enter location of bookmarks.html")
location = input()
if not os.path.isdir(location):
    print("Invalid location!")
    sys.exit()
os.chdir(location)

myBookSoup = bs4.BeautifulSoup(open("bookmarks.html"), "html.parser")
textElem = myBookSoup.select('a')

myFile = open("bookmarks.txt", "w")
for i in range(len(textElem)):
    myFile.write("[" + str(i) + "] " + textElem[i].getText() + "\n")
myFile.close()

Comments about the code are welcome.irefox

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s