使用python,从字符串中删除HTML标签/格式[复制]

时间:2021-11-27 04:23:25

This question already has an answer here:

这个问题在这里已有答案:

I have a string that contains html markup like links, bold text, etc.

我有一个包含html标记的字符串,如链接,粗体文本等。

I want to strip all the tags so I just have the raw text.

我想剥离所有标签,所以我只有原始文本。

What's the best way to do this? regex?

最好的方法是什么?正则表达式?

5 个解决方案

#1


30  

If you are going to use regex:

如果你打算使用正则表达式:

import re
def striphtml(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

>>> striphtml('<a href="foo.com" class="bar">I Want This <b>text!</b></a>')
'I Want This text!'

#2


11  

AFAIK using regex is a bad idea for parsing HTML, you would be better off using a HTML/XML parser like beautiful soup.

使用正则表达式的AFAIK对于解析HTML是一个坏主意,你最好使用像美丽的汤这样的HTML / XML解析器。

#3


8  

Use lxml.html. It's much faster than BeautifulSoup and raw text is a single command.

使用lxml.html。它比BeautifulSoup快得多,原始文本只是一个命令。

>>> import lxml.html
>>> page = lxml.html.document_fromstring('<!DOCTYPE html>...</html>')
>>> page.cssselect('body')[0].text_content()
'...'

#4


3  

Use SGMLParser. regex works in simple case. But there are a lot of intricacy with HTML you rather not have to deal with.

使用SGMLParser。正则表达式工作简单。但是你有很多复杂的HTML而不必处理。

>>> from sgmllib import SGMLParser
>>>
>>> class TextExtracter(SGMLParser):
...     def __init__(self):
...         self.text = []
...         SGMLParser.__init__(self)
...     def handle_data(self, data):
...         self.text.append(data)
...     def getvalue(self):
...         return ''.join(ex.text)
...
>>> ex = TextExtracter()
>>> ex.feed('<html>hello &gt; world</html>')
>>> ex.getvalue()
'hello > world'

#5


-1  

Depending on whether the text will contain '>' or '<' I would either just make a function to remove anything between those, or use a parsing lib

根据文本是否包含'>'或'<',我或者只是创建一个函数来删除它们之间的任何内容,或者使用解析库

def cleanStrings(self, inStr):
  a = inStr.find('<')
  b = inStr.find('>')
  if a < 0 and b < 0:
    return inStr
  return cleanString(inStr[a:b-a])

#1


30  

If you are going to use regex:

如果你打算使用正则表达式:

import re
def striphtml(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

>>> striphtml('<a href="foo.com" class="bar">I Want This <b>text!</b></a>')
'I Want This text!'

#2


11  

AFAIK using regex is a bad idea for parsing HTML, you would be better off using a HTML/XML parser like beautiful soup.

使用正则表达式的AFAIK对于解析HTML是一个坏主意,你最好使用像美丽的汤这样的HTML / XML解析器。

#3


8  

Use lxml.html. It's much faster than BeautifulSoup and raw text is a single command.

使用lxml.html。它比BeautifulSoup快得多,原始文本只是一个命令。

>>> import lxml.html
>>> page = lxml.html.document_fromstring('<!DOCTYPE html>...</html>')
>>> page.cssselect('body')[0].text_content()
'...'

#4


3  

Use SGMLParser. regex works in simple case. But there are a lot of intricacy with HTML you rather not have to deal with.

使用SGMLParser。正则表达式工作简单。但是你有很多复杂的HTML而不必处理。

>>> from sgmllib import SGMLParser
>>>
>>> class TextExtracter(SGMLParser):
...     def __init__(self):
...         self.text = []
...         SGMLParser.__init__(self)
...     def handle_data(self, data):
...         self.text.append(data)
...     def getvalue(self):
...         return ''.join(ex.text)
...
>>> ex = TextExtracter()
>>> ex.feed('<html>hello &gt; world</html>')
>>> ex.getvalue()
'hello > world'

#5


-1  

Depending on whether the text will contain '>' or '<' I would either just make a function to remove anything between those, or use a parsing lib

根据文本是否包含'>'或'<',我或者只是创建一个函数来删除它们之间的任何内容,或者使用解析库

def cleanStrings(self, inStr):
  a = inStr.find('<')
  b = inStr.find('>')
  if a < 0 and b < 0:
    return inStr
  return cleanString(inStr[a:b-a])