【问题标题】:extracting paragraph in python using lxml使用lxml在python中提取段落
【发布时间】:2011-06-29 09:26:38
【问题描述】:

我想通过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>

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

【问题讨论】:

    标签: python html-parsing lxml paragraphs


    【解决方案1】:
    print html.parse(url).xpath('//p/text()')
    

    输出

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

    【讨论】:

    • 你能写更多关于它背后的逻辑吗?! '//p/text()'
    • @user611748: text() 只选择直接文本节点。尝试在文本中添加cmets&lt;!-- comment --&gt;&lt;b&gt;First&lt;/b&gt;
    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2019-05-05
    • 1970-01-01
    相关资源
    最近更新 更多