lxml cssselect解析

2024-09-29 02:17:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份资料如下:

<div class="ds-list">
    <b>1. </b> 
    A domesticated carnivorous mammal 
    <i>(Canis familiaris)</i> 
    related to the foxes and wolves and raised in a wide variety of breeds.
</div>

我想得到类ds-list(没有<b><i>标记)中的所有内容。目前我的代码是doc.cssselect('div.ds-list'),但所有这些都是<b>之前的新行。我怎样才能让它做我想做的事?


Tags: andthetodivdslistclassrelated
2条回答

也许您正在寻找text_content方法?以下内容:

import lxml.html as lh
content='''\
<div class="ds-list">
    <b>1. </b> 
    A domesticated carnivorous mammal 
    <i>(Canis familiaris)</i> 
    related to the foxes and wolves and raised in a wide variety of breeds.
</div>'''
doc=lh.fromstring(content)
for div in doc.cssselect('div.ds-list'):
    print(div.text_content())

收益率

1.  
A domesticated carnivorous mammal 
(Canis familiaris) 
related to the foxes and wolves and raised in a wide variety of breeds.
doc.cssselect("div.ds-list").text_content()

相关问题 更多 >