Goodreads Developers discussion
questions
>
how to use python elementtree to extract list of my shelves a book is on.
date
newest »


#!/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)
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