Search for a book using Python and Firefox

Get book information from command line arguments or clipboard, open a web browser, and search on Amazon and the Toronto Public Library (TPL).

When I read something interesting online about a book or author I start to dig deeper by checking out details on Amazon and availability at my local library. Is it possible that Python can improve on this search flow? Why yes it can!

I discovered two new Python modules and one new concept:

Step 0: On Ubuntu/Debian for Python3, install ...

$ sudo apt install python3-pyperclip xclip

Step 1: Create a Python module [source] that opens new tabs and URLs in Firefox ...

import webbrowser, pyperclip

class Browser():
    """Super browser powers"""

    def __init__(self):
        """Set a default browser."""
        self.browser = 'firefox'

    def open_search_tab(self, web_address):
        Open browser tab for address retrieved from command line arguments
        or clipboard
        if len(sys.argv) > 1:
            # Get search item from command line
            search_item = ' '.join(sys.argv[1:])
            # Get search item from clipboard
            search_item = pyperclip.paste()

        if self.browser in webbrowser._browsers:
            www = webbrowser.get(self.browser).open
            www =

        www(web_address + search_item)

Step 2: Setup a script [source] to call the module, open tabs in the browser, and run searches on Amazon.(com,ca) and the TPL ...

#!/usr/bin/env python3
from bernersLee import Browser

def main():
    library = ""
    amazonCa = ""
    amazonCom = ""
    amzSearch = "url=search-alias%3Dstripbooks&field-keywords="

    browser = Browser()
    browser.open_search_tab(amazonCa + amzSearch)
    browser.open_search_tab(amazonCom + amzSearch)


Cut-and-paste text in the browser, launch from the run menu, and all the tabs+searches open and go to work!

Happy hacking!

More • firefoxpythonprogramming