【问题标题】:Remove specific element from html page using lxml python etree使用 lxml python etree 从 html 页面中删除特定元素
【发布时间】:2013-11-24 06:45:12
【问题描述】:

我想在 xml 树中加载页面的 html 内容,并在 python 中使用 lxml 删除其中的元素。我只想知道如何从内容中删除元素?

【问题讨论】:

    标签: python lxml elementtree


    【解决方案1】:

    您可以使用BeautifulSoup4 和lxml 的组合来轻松实现您的目标。

    将您的 HTML 解析为树 / 汤。您只需要安装所有组件并执行此操作。

    from bs4 import BeautifulSoup
    html = """..."""
    soup = BeautifulSoup(html, 'lxml')
    ...
    

    你修改了树,这里有一个完整的参考列表,教你如何修改标签的内容/属性等。

    BeautifulSoup/Modify The tree

    这里是an example我确实修改了anchor标签的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 2013-06-27
      • 1970-01-01
      • 2014-03-13
      相关资源
      最近更新 更多