开发者

lxml cssselect Parsing

开发者 https://www.devze.com 2023-02-09 00:03 出处:网络
I have a document with the following data: <div class=\"d开发者_如何转开发s-list\"> <b>1. </b>

I have a document with the following data:

<div class="d开发者_如何转开发s-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>

And I want to get everything within the class ds-list (without <b> and <i> tags). Currently my code is doc.cssselect('div.ds-list'), but all this picks up is the newline before the <b>. How can I get this to do what I want it to?


Perhaps you are looking for the text_content method?:

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())

yields

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()
0

精彩评论

暂无评论...
验证码 换一张
取 消