Python 解析:lxml 仅获取标签文本的一部分

发布于 2024-09-10 16:17:23 字数 326 浏览 9 评论 0原文

我正在使用 Python 和 HTML 进行工作,如下所示。我正在使用 lxml 进行解析,但同样可以愉快地使用 pyquery:

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

无论我使用什么库,提取“名称”和“地址”都非常容易,但是如何获取文本的其余部分 - 即“戴夫·戴维斯”?

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'?

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

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

发布评论

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

评论(3

季末如歌 2024-09-17 16:17:23

另一种方法——使用 xpath:

>>> from lxml import html
>>> doc = html.parse( file )
>>> doc.xpath( '//span[@class="Title"][text()="Name"]/../self::p/text()' )
['Dave Davies']
>>> doc.xpath( '//span[@class="Title"][text()="Address"]/../self::p/text()' )
['123 Greyfriars Road, London']

Another method -- using xpath:

>>> from lxml import html
>>> doc = html.parse( file )
>>> doc.xpath( '//span[@class="Title"][text()="Name"]/../self::p/text()' )
['Dave Davies']
>>> doc.xpath( '//span[@class="Title"][text()="Address"]/../self::p/text()' )
['123 Greyfriars Road, London']
墨落成白 2024-09-17 16:17:23

每个元素可以有一个 文本和一个 tail 属性(在链接中,搜索单词“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')

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')
冰魂雪魄 2024-09-17 16:17:23

看看BeautifulSoup。我刚刚开始使用它,所以我不是专家。我的头顶上浮现出:

import BeautifulSoup

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

soup = BeautifulSoup.BeautifulSoup(text)

paras = soup.findAll('p')

for para in paras:
    spantext = para.span.text
    othertext = para.span.nextSibling
    print spantext, othertext

[Out]: Name Dave Davies
       Address 123 Greyfriars Road, London

Have a look at BeautifulSoup. I've just started using it, so I'm no expert. Off the top of my head:

import BeautifulSoup

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

soup = BeautifulSoup.BeautifulSoup(text)

paras = soup.findAll('p')

for para in paras:
    spantext = para.span.text
    othertext = para.span.nextSibling
    print spantext, othertext

[Out]: Name Dave Davies
       Address 123 Greyfriars Road, London
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文