如何使用 lxml 、 python 解析 html

发布于 2024-10-02 14:48:54 字数 294 浏览 4 评论 0原文

我有一些 html 文件:

<html>
 <body>
   <span class="text">One</span>some text1</br>
   <span class="cyrillic">Мир</span>some text2</br>
 </body>
</html>

如何使用 lxml 和 python 获取“some text1”和“some text2”?

I have some html file:

<html>
 <body>
   <span class="text">One</span>some text1</br>
   <span class="cyrillic">Мир</span>some text2</br>
 </body>
</html>

How can i get "some text1" and "some text2" using lxml with python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心小果奶 2024-10-09 14:48:54
import lxml.html

doc = lxml.html.document_fromstring("""<html>
 <body>
   <span class="text">One</span>some text1</br>
   <span class="cyrillic">Мир</span>some text2</br>
 </body>
</html>
""")

txt1 = doc.xpath('/html/body/span[@class="text"]/following-sibling::text()[1]')
txt2 = doc.xpath('/html/body/span[@class="cyrillic"]/following-sibling::text()[1]')
import lxml.html

doc = lxml.html.document_fromstring("""<html>
 <body>
   <span class="text">One</span>some text1</br>
   <span class="cyrillic">Мир</span>some text2</br>
 </body>
</html>
""")

txt1 = doc.xpath('/html/body/span[@class="text"]/following-sibling::text()[1]')
txt2 = doc.xpath('/html/body/span[@class="cyrillic"]/following-sibling::text()[1]')
甜点 2024-10-09 14:48:54

我使用 lxml 进行 xml 解析,但我使用 BeautifulSoup 进行 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
>>> 

I use lxml for xml parsing, but I use BeautifulSoup for HTML. Here's a very quick/brief tour, ending with one solution to your question. Hope it helps.

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
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文