日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發(fā)布時間:2025/3/12 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python restful django_如何使用Django / Python从RESTful Web服务中使用XML? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我應該使用PyXML還是標準庫中的內(nèi)容?

解決方法:

ElementTree是標準Python庫的一部分. ElementTree是純python,而cElementTree是更快的C實現(xiàn):

# 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

這是一個示例用法,我從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

標簽:python,rest,xml,django

來源: https://codeday.me/bug/20190715/1464351.html

總結

以上是生活随笔為你收集整理的python restful django_如何使用Django / Python从RESTful Web服务中使用XML?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。