2

我想通过python提取html中的段落。我使用了 lxml 模块,但它并没有完全符合我的要求。

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>

我应该补充一点,在不同的页面中,我有不同数量的段落,所以想列出一个列表,然后将段落放入其中。

4

1 回答 1

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

输出

['Here is the First Paragraph.', 'Here is the second Paragraph.', 
 'Paragraph Three."']
于 2011-02-17T20:52:48.287 回答