Parse XML from URL into python object

前端 未结 3 1139
轮回少年
轮回少年 2021-01-31 04:15

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

3条回答
  •  -上瘾入骨i
    2021-01-31 04:37

    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})
    

提交回复
热议问题