lxml cssselect解析 [英] lxml cssselect Parsing

查看:307
本文介绍了lxml cssselect解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数据的文档:

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>

并且我想获得类中的所有内容 ds-list (不含< b> < i> 标签)。目前我的代码是 doc.cssselect('div.ds-list'),但所有这些选择是< b&

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?

推荐答案

也许你正在寻找 text_content 方法?:

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

产生

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

这篇关于lxml cssselect解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆