0

I have a list of authors and books and I'm using an API to retrieve the descriptions and ratings of each of them. I'm not able to iterate the list and store the descriptions in a new list.

The Goodreads API gives me a chance to look for books' information, sending the title and author for the accuracy, or only the title. What I want is:

  1. Loop the list of titles and authors, get the 1st title and 1st author and try to retrieve the description and save it in a third list.
  2. If not found, try to retrieve the description using only the title and save it to the list.
  3. If not found yet, add a standard error message.

I've tried the code below, but I'm not able to iterate the entire list and save the results.

#Running the API with author's name and book's title
book_url_w_author = 'https://www.goodreads.com/book/title.xml?author='+edit_authors[0]+'&key='+api_key+'&title='+edit_titles[0]

#Running the API with only book's title
book_url_n_author = 'https://www.goodreads.com/book/title.xml?'+'&key='+api_key+'&title='+edit_titles[0]

# parse book url with author
html_n_author = requests.get(book_url_w_author).text
soup_n_author = BeautifulSoup(html_n_author, "html.parser")

# parse book url without author
html_n_author = requests.get(book_url_n_author).text
soup_n_author = BeautifulSoup(html_n_author, "html.parser")

#Retrieving the books' descriptions
description = []

try:
    #fetch description for url with author and title and add it to descriptions list
    for desc_w_author in soup_w_author.book.description:
        description.append(desc_w_author)
except:
    #fetch description for url with only title and add it to descriptions list
    for desc_n_author in soup_n_author.book.description:
        description.append(desc_n_author)
else:
    #return and inform that no description was found
    description.append('Description not found')

Expected:
description = [description1, description2, description3, ....]
aschultz
  • 1,658
  • 3
  • 20
  • 30
lela_rib
  • 147
  • 2
  • 10
  • My initial impression is that you won't get any errors in the try loop, even if there is nothing in soup_w_author.book.description. Python just sees an empty array and says ok, there's nothing to do here--but it never throws an error. So you'll never get to the except loop. I think what you may want is separate if loops e.g. if len(soup_w_author.book.description/): (1st for loop) elif len (soup_n_author.book.description/): (2nd for loop) else (append "description not found") – aschultz Aug 11 '19 at 15:37
  • `soup_w_author` is not defined, so it looks like your first loop is throwing a NameError and you're blindly catching it. Please fix that, remove the [bare `except`](https://stackoverflow.com/a/14797508/4518341), and if the issue is still occurring, make a [mre] and [edit] it into your post. For now I'm voting to close this as a typo. – wjandrea Aug 11 '19 at 17:20
  • I was able to solve the problem using a dict with authors and titles an then looping the dict. Thanks for the help! – lela_rib Aug 12 '19 at 18:22

0 Answers0