37

The goodreads website has this API for accessing a user's 'shelves:' https://www.goodreads.com/review/list/20990068.xml?key=nGvCqaQ6tn9w4HNpW8kquw&v=2&shelf=toread

It returns XML. I'm trying to create a django project that shows books on a shelf from this API. I'm looking to find out how (or if there is a better way than) to write my view so I can pass an object to my template. Currently, this is what I'm doing:

import urllib2

def homepage(request):
    file = urllib2.urlopen('https://www.goodreads.com/review/list/20990068.xml?key=nGvCqaQ6tn9w4HNpW8kquw&v=2&shelf=toread')
    data = file.read()
    file.close()
    dom = parseString(data)

I'm not entirely sure how to manipulate this object if I'm doing this correctly. I'm following this tutorial.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
smilebomb
  • 5,123
  • 8
  • 49
  • 81

3 Answers3

48

I'd use xmltodict to make a python dictionary out of the XML data structure and pass this dictionary to the template inside the context:

import urllib2
import xmltodict

def homepage(request):
    file = urllib2.urlopen('https://www.goodreads.com/review/list/20990068.xml?key=nGvCqaQ6tn9w4HNpW8kquw&v=2&shelf=toread')
    data = file.read()
    file.close()

    data = xmltodict.parse(data)
    return render_to_response('my_template.html', {'data': data})
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
12

xmltodict using urllib3

import traceback
import urllib3
import xmltodict

def getxml():
    url = "https://yoursite/your.xml"

    http = urllib3.PoolManager()

    response = http.request('GET', url)
    try:
        data = xmltodict.parse(response.data)
    except:
        print("Failed to parse xml from response (%s)" % traceback.format_exc())
    return data
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
gies0r
  • 4,723
  • 4
  • 39
  • 50
12

xmltodict using requests

import requests
import xmltodict

url = "https://yoursite/your.xml"
response = requests.get(url)
data = xmltodict.parse(response.content)
Vincent
  • 857
  • 7
  • 6