lxml cssselect解析

Joh*_*ard 2 html python parsing lxml css-selectors

我有一份包含以下数据的文件:

<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>
Run Code Online (Sandbox Code Playgroud)

我希望得到课堂上的所有内容ds-list(没有<b><i>标签).目前我的代码是doc.cssselect('div.ds-list'),但所有这一切都是之前的新行<b>.我怎样才能让它做我想做的事情?

unu*_*tbu 8

也许您正在寻找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())
Run Code Online (Sandbox Code Playgroud)

产量

1.  
A domesticated carnivorous mammal 
(Canis familiaris) 
related to the foxes and wolves and raised in a wide variety of breeds.
Run Code Online (Sandbox Code Playgroud)