使用lxml在python中提取段落

使用lxml在python中提取段落

本文介绍了使用lxml在python中提取段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用python提取html中的段落.我使用了lxml模块,但它并不能完全满足我的需求.

I would like to extract paragraphs in html by python. I used lxml module but it doesn't do exactly what I am looking for.

print html.parse(url).xpath('//p')[1].text_content()

<span id="midArticle_1"></span><p>Here is the First Paragraph.</p><span id="midArticle_2"></span><p>Here is the second Paragraph.</p><span id="midArticle_3"></span><p>Paragraph Three."</p>

我应该补充一点,在不同的页面中,我有不同的段落数,因此想列出一个列表并将其放在其中.

I should add that, in different pages I have different number of paragraph, so would like to make a list and put paragraph into it after that.

推荐答案

print html.parse(url).xpath('//p/text()')

输出

['Here is the First Paragraph.', 'Here is the second Paragraph.',
 'Paragraph Three."']

这篇关于使用lxml在python中提取段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:11