Python解析:lxml仅获取标签文本的一部分 [英] Python parsing: lxml to get just part of a tag's text

查看:370
本文介绍了Python解析:lxml仅获取标签文本的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python编写类似如下的HTML.我正在使用lxml进行解析,但同样可以愉快地使用pyquery:

I'm working in Python with HTML that looks like this. I'm parsing with lxml, but could equally happily use pyquery:

<p><span class="Title">Name</span>Dave Davies</p>
<p><span class="Title">Address</span>123 Greyfriars Road, London</p>

无论使用哪种库,都很难拔出名称"和地址",但是如何获得文本的其余部分-即戴夫·戴维斯"?

Pulling out 'Name' and 'Address' is dead easy, whatever library I use, but how do I get the remainder of the text - i.e. 'Dave Davies'?

推荐答案

每个元素都可以具有

Each Element can have a text and a tail attribute (in the link, search for the word "tail"):

import lxml.etree

content='''\
<p><span class="Title">Name</span>Dave Davies</p>
<p><span class="Title">Address</span>123 Greyfriars Road, London</p>'''


root=lxml.etree.fromstring(content,parser=lxml.etree.HTMLParser())
for elt in root.findall('**/span'):
    print(elt.text, elt.tail)

# ('Name', 'Dave Davies')
# ('Address', '123 Greyfriars Road, London')

这篇关于Python解析:lxml仅获取标签文本的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆