擅长:python、mysql、java
<p>如果要解析内存中的对象(在本例中是字符串),请使用<code>etree.fromstring(<obj>)</code><code>etree.parse</code>需要类似文件的对象或文件名<a href="http://lxml.de/parsing.html#parsers" rel="nofollow">Docs</a></p>
<p>例如:</p>
<pre><code>import urllib2, lxml.etree as etree
url = 'http://www.xmlfiles.com/examples/note.xml'
headers = {}
req = urllib2.Request(url, headers=headers)
responseData = urllib2.urlopen(req).read()
element = etree.fromstring(responseData)
print(element)
print(etree.tostring(element, pretty_print=True))
</code></pre>
<p>输出:</p>
<pre><code><Element note at 0x2c29dc8>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</code></pre>