简体   繁体   English

如何使用Django / Python从RESTful Web服务中使用XML?

[英]How to consume XML from RESTful web services using Django / Python?

我应该使用PyXML还是标准库中的内容?

ElementTree is provided as part of the standard Python libs. ElementTree是标准Python库的一部分。 ElementTree is pure python, and cElementTree is the faster C implementation: ElementTree是纯python,而cElementTree是更快的C实现:

# Try to use the C implementation first, falling back to python
try:
    from xml.etree import cElementTree as ElementTree
except ImportError, e:
    from xml.etree import ElementTree

Here's an example usage, where I'm consuming xml from a RESTful web service: 这是一个示例用法,我从RESTful Web服务中使用xml:

def find(*args, **kwargs):
    """Find a book in the collection specified"""

    search_args = [('access_key', api_key),]
    if not is_valid_collection(kwargs['collection']):
        return None
    kwargs.pop('collection')
    for key in kwargs:
        # Only the first keword is honored
        if kwargs[key]:
            search_args.append(('index1', key))
            search_args.append(('value1', kwargs[key]))
            break

    url = urllib.basejoin(api_url, '%s.xml' % 'books')
    data = urllib.urlencode(search_args)
    req = urllib2.urlopen(url, data)
    rdata = []
    chunk = 'xx'
    while chunk:
        chunk = req.read()
        if chunk:
            rdata.append(chunk)
    tree = ElementTree.fromstring(''.join(rdata))
    results = []
    for i, elem in enumerate(tree.getiterator('BookData')):
        results.append(
               {'isbn': elem.get('isbn'),
                'isbn13': elem.get('isbn13'),
                'title': elem.find('Title').text,
                'author': elem.find('AuthorsText').text,
                'publisher': elem.find('PublisherText').text,}
             )
    return results

I always prefer to use the standard library when possible. 我总是喜欢尽可能使用标准库。 ElementTree is well known amongst pythonistas, so you should be able to find plenty of examples. ElementTree在pythonistas中是众所周知的,所以你应该能够找到很多例子。 Parts of it have also been optimized in C, so it's quite fast. 部分内容也在C中进行了优化,因此速度非常快。

http://docs.python.org/library/xml.etree.elementtree.html http://docs.python.org/library/xml.etree.elementtree.html

There's also BeautifulSoup , which has an API some might prefer. 还有BeautifulSoup ,它有一些API可能更喜欢。 Here's an example on how you can extract all tweets that have been favorited from Twitter's Public Timeline: 以下是一个示例,说明如何从Twitter的公共时间线中提取所有已被收藏的推文:

from BeautifulSoup import BeautifulStoneSoup
import urllib

url = urllib.urlopen('http://twitter.com/statuses/public_timeline.xml').read()
favorited = []

soup = BeautifulStoneSoup(url)
statuses = soup.findAll('status')

for status in statuses:
    if status.find('favorited').contents != [u'false']:
        favorited.append(status)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM