将URL从URL解析为python对象

时间:2014-06-09 16:31:02

标签: python xml django xml-parsing urllib2

goodreads网站有此API用于访问用户的“货架:”https://www.goodreads.com/review/list/20990068.xml?key=nGvCqaQ6tn9w4HNpW8kquw&v=2&shelf=toread

它返回XML。我正在尝试创建一个django项目,该项目通过此API显示书架上的书籍。我正在寻找如何(或者是否有更好的方法)来编写我的视图,以便将对象传递给我的模板。目前,这就是我正在做的事情:

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)

如果我正确地执行此操作,我不完全确定如何操纵此对象。我正在关注tutorial

3 个答案:

答案 0 :(得分:33)

我使用xmltodictXML数据结构中创建python字典,并将此字典传递给上下文中的模板:

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

答案 1 :(得分:2)

xmltodict使用requests

import requests
import xmltodict

url = "https://yoursite/your.xml"
response = requests.get(url)
data = xmltodict.parse(response.content)

答案 2 :(得分:1)

xmltodict使用urllib3

import traceback
import urllib3
import xmltodict

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


    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