我应该使用PyXML还是标准库中的内容?
发布于 2009-04-30 01:19:01
ElementTree是作为标准Python库的一部分提供的。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下面是一个示例用法,其中我使用来自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发布于 2009-04-30 01:17:46
只要有可能,我总是更喜欢使用标准库。ElementTree在pythonistas中非常出名,因此您应该能够找到大量的示例。它的一部分也已经用C语言进行了优化,所以它的速度相当快。
http://docs.python.org/library/xml.etree.elementtree.html
发布于 2010-02-27 21:43:17
还有BeautifulSoup,它有一些人可能更喜欢的API。下面是一个例子,告诉你如何从Twitter的Public Timeline中提取出所有最受欢迎的推文:
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)https://stackoverflow.com/questions/804992
复制相似问题