Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在LXML中,如何保留标记,但删除所有内联样式等?_Python_Html_Lxml_Lxml.html - Fatal编程技术网

Python 在LXML中,如何保留标记,但删除所有内联样式等?

Python 在LXML中,如何保留标记,但删除所有内联样式等?,python,html,lxml,lxml.html,Python,Html,Lxml,Lxml.html,我知道我可以用LXML删除标签,如等,但如何让它保留标签,同时删除其他所有内容,如: <h1 style='font-size: 24px; margin: 0px 0px 12px; font-weight: 600;&#13; line-height: 28px; color: rgb(23, 43, 77); font-family:&#13; -apple-system, BlinkMacSystemFont, "Segoe UI", Rob

我知道我可以用LXML删除标签,如
等,但如何让它保留标签,同时删除其他所有内容,如:

<h1 style='font-size: 24px; margin: 0px 0px 12px; font-weight: 600;&#13;
      line-height: 28px; color: rgb(23, 43, 77); font-family:&#13;
      -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,&#13;
      "Noto Sans", Ubuntu, "Droid Sans",&#13;
      "Helvetica Neue", sans-serif; font-style: normal;&#13;
      font-variant-ligatures: normal; font-variant-caps: normal;&#13;
      letter-spacing: normal; orphans: 2; text-align: start;&#13;
      text-indent: 0px; text-transform: none; white-space: normal;&#13;
      widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;&#13;
      background-color: rgb(244, 245, 247); text-decoration-style:&#13;
      initial; text-decoration-color: initial;'>This text must stay</h1>&#13;

你说的“标签类”是什么意思?你的样本中没有类。你是对的,我没有包括那个样本。一些未包含的数据中包含了类信息,但我怀疑这个想法很清楚。我想要标签,里面没有类、样式等任何东西。如果样本更具代表性,想法会更清晰。问题是什么并不明显。这是公平的,更多的例子会更好。我相信问题的这一部分展示了一个例子h1和所有的数据,然后展示了它在
之后需要的样子。这篇文章必须保持
基本上说明了需要什么。
safe_attrs = list(lxml.html.clean.defs.safe_attrs) + ['bgcolor']
        lxml.html.clean.defs.safe_attrs = safe_attrs
        lxml.html.clean.Cleaner.safe_attrs = lxml.html.clean.defs.safe_attrs
        cleaner = lxml.html.clean.Cleaner(style=False, remove_tags=['body', 'pre'], safe_attrs_only=True,
                                          safe_attrs=safe_attrs, kill_tags=['title', 'code'])
        return cleaner.clean_html(data)