The Goodreads API suffers from good documentation and lack of features. Currently, it is not possible for a user to get a list of their saved books. Furthermore, when you navigate to the section
My Books, you need to keep scrolling to the end of the page until all books are loaded in the table. I wrote a small script in
pandas to generate a csv file that contains all my saved books.
- Download the Selenium driver (in my code I am using Chrome, but you can easily switch to Firefox)
- When Selenium opens your profile, a pop-up will show up asking for your login credentials. The code will ignore the pop-up by refreshing the page, however in order to see the list of your books either stay logged-in in Chrome or make your profile public.
Start by importing the necessary libraries.
from selenium import webdriver
Initialize the webdriver element on the chrome browser and specify the url you are trying to access.
driver = webdriver.Chrome()
Wait for pop-up to show up (2 seconds) then refresh the page.
The goal is to stop scrolling when the table has loaded all your saved books. We can do this by accessing the HTML element at the bottom of the page which indicated the number of books loaded:
60 of 995 loaded. We strip the string from any leading or trailing whitespaces, then we split the string on spaces. The first element of this list is the current number of books loaded. The third element is the maximum number of books in our list.
s = driver.find_element_by_id('infiniteStatus').text.strip()
Next we loop until the end is reached and the table is filled with all books.
while (current < maxx):
Once the page is loaded, we get the source code and search for a table with the id ‘books’. Pandas has a built in feature that converts table tags into a dataframe.
html_source = driver.page_source
We keep two columns (title and author), clean the strings, and write to a csv file.
df = df[['title','author']]
The output is a csv file which contains all titles and authors of the books you have saved on Goodreads.