我使用lxml进行xml解析,但我使用BeautifulSoup for HTML。这是一个非常快速/简短的旅程,以您的问题的一个解决方案结束。希望能帮助到你。
Python 2.6.5 (r265:79359, Mar 24 2010, 01:32:55)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from BeautifulSoup import BeautifulSoup as soup
>>> stream = open('bs.html', 'r')
>>> doc = soup(stream.read())
>>> doc.body.span
<span class="text">One</span>
>>> doc.body.span.nextSibling
u'some text1'
>>> x = doc.findAll('span')
>>> for i in x:
... print unicode(i)
...
<span class="text">One</span>
<span class="cyrillic">Мир</span>
>>> x = doc('span')
>>> type(x)
<class 'BeautifulSoup.ResultSet'>
>>> for i in x:
... print unicode(i)
...
<span class="text">One</span>
<span class="cyrillic">Мир</span>
>>> for i in x:
... print i.nextSibling
...
some text1
some text2
>>>
这里的教程:http://codespeak.net/lxml/tutorial.html具体 任何你不明白吗? – Wolph 2010-11-15 02:38:20
本教程链接已停用。请删除。 – ely 2013-04-23 21:18:17