Goodreads Developers discussion

60 views
questions > how to use python elementtree to extract list of my shelves a book is on.

Comments Showing 1-3 of 3 (3 new)    post a comment »
dateUp arrow    newest »

message 1: by Shane (new)

Shane Phillips | 25 comments I am using Python to extract my goodreads data into a sqlite db. In Python, I use "ElementTree" to extract the Goodreads API. However, I cannot find out how to get my list of shelves that I have a book on.
I can get the other detail (title, shelf, year, etc) but there is something about those embedded tags that I am missing.

note: added a space so goodreads would not process xml.
-------sample----------
< spoilers_state>none
< shelves>
< shelf exclusive="false" id="153934772" name="full-list" review_shelf_id="2562838816" sortable="true"/>
< shelf exclusive="true" id="269364260" name="s-harry_bosch" review_shelf_id="2562838881" sortable="true"/>
< /shelves>
< recommended_for/>
---------end sample

I am not sure how to get the shelf name and shelf_id out of these tags.

any help?

API method:
https://www.goodreads.com/review/list...

thanks


message 2: by Matt (new)

Matt | 6 comments

#!/usr/bin/python3

import re
from xml.etree import ElementTree

import requests


USER_ID = 10_052_745
API_KEY = "[% REDACTED %]"

r = requests.get(f"https://www.goodreads.com/review/list...", params={
"key": API_KEY,
"v": 2,
"per_page": 10,
"page": 1
})
x = ElementTree.fromstring(r.content)

for r in x.findall("reviews/"):
print("title:", r.find("book/title_without_series").text)
print("exclusive shelf:", r.find("shelves/shelf[@exclusive='true']").get("name"))
print("all shelves:")
for shelf in r.findall("shelves/"):
shelf_name = shelf.get("name")
shelf_id = shelf.get("id")
print(f"* {shelf_name} ({shelf_id})")

print()

# vim: ts=4 : sw=4 : et


that'll produce something like this:


title: Nightblind
exclusive shelf: library
all shelves:
* library (264428736)
* borrowed (219480356)

title: Island
exclusive shelf: to-read
all shelves:
* to-read (33356208)



message 3: by Shane (new)

Shane Phillips | 25 comments THANK YOU Matt. That worked perfectly!!


back to top