Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.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替换HTML标记_Python_Html_Regex_Tags - Fatal编程技术网

使用正则表达式和python替换HTML标记

使用正则表达式和python替换HTML标记,python,html,regex,tags,Python,Html,Regex,Tags,我有一个Python脚本,它将查看具有以下格式的HTML文件: <DOC> <HTML> ... </HTML> </DOC> <DOC> <HTML> ... </HTML> </DOC> ... ... 除了在Python中使用正则表达式打开和关闭文档标记外,如何删除所有HTML标记(用“”替换标记)?另外,如果我想保留标记的alt文本,正则表达式应该是什么样子?搜索并替换为这个正则表达式:

我有一个Python脚本,它将查看具有以下格式的HTML文件:

<DOC>
<HTML>
...
</HTML>
</DOC>
<DOC>
<HTML>
...
</HTML>
</DOC>

...
...

除了在Python中使用正则表达式打开和关闭文档标记外,如何删除所有HTML标记(用“”替换标记)?另外,如果我想保留标记的alt文本,正则表达式应该是什么样子?

搜索并替换为这个正则表达式:搜索:替换为:“

搜索并替换为这个正则表达式:搜索:替换为:”

签出,这是一个处理xml的非常好的python库。您可以使用drop_标记来完成所需的内容

from lxml import html h = html.fragment_fromstring('<doc>Hello <b>World!</b></doc>') h.find('*').drop_tag() print(html.tostring(h, encoding=unicode)) <doc>Hello World!</doc> 从lxml导入html h=html.fragment\u fromstring('helloworld!') h、 查找('*')。删除标签() 打印(html.tostring(h,编码=unicode)) 你好,世界! 看看,这是一个处理xml的非常好的python库。您可以使用drop_标记来完成所需的内容

from lxml import html h = html.fragment_fromstring('<doc>Hello <b>World!</b></doc>') h.find('*').drop_tag() print(html.tostring(h, encoding=unicode)) <doc>Hello World!</doc> 从lxml导入html h=html.fragment\u fromstring('helloworld!') h、 查找('*')。删除标签() 打印(html.tostring(h,编码=unicode)) 你好,世界!
对于您试图实现的目标,我将使用BeautifulSoup而不是正则表达式


对于您想要实现的目标,我将使用BeautifulSoup而不是正则表达式


您应该使用DOM解析器,而不是正则表达式。请参阅,您可以更具体地说明要删除的内容吗?我想删除除和标记以外的所有标记。“html”是一个假设的元素名称,而不是真正的“html”吗?您应该使用DOM解析器,而不是正则表达式。请参阅,您可以更具体地说明要删除的内容吗?我要删除除和标记之外的所有标记。“html”是一个假设的元素名称,而不是真正的“html”吗?