lxml cssselect Parsing

14,570

Solution 1

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.

Solution 2

doc.cssselect("div.ds-list").text_content()
Share:
14,570
John Howard
Author by

John Howard

Updated on June 04, 2022

Comments

  • John Howard
    John Howard almost 2 years

    I have a document with the following data:

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

    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?