gpt4 book ai didi

python - 将 HTML 解析为纯文本

转载 作者:行者123 更新时间:2023-11-30 23:39:37 26 4
gpt4 key购买 nike

我正在尝试使用我发现在多个帖子中推荐的 MLStripper 类来从电子邮件中删除 html 以获得纯文本。由于“@”符号,strip_tags 函数在尝试解析时会遇到问题。我想这个类不够强大,只能解析有效的 html 标签,关于如何修复以下内容以处理“@”或另一个库以从文本中删除 html 有什么建议吗?我还需要删除诸如 & 之类的东西。

Python:

from HTMLParser import HTMLParser

class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)

def strip_tags(self, html):
s = MLStripper()
s.feed(html)
return s.get_data()

ML = MLStripper()
test = ML.strip_tags("<div><br>On Sep 27, 2012, at 4:11 PM, Mark Smith <marksmith@gmail.com> wrote</br></div>")
print test

错误:

Traceback (most recent call last):
File "IMAPReader.py", line 49, in <module>
strippedText = ML.strip_tags("<marksmith@gmail.com>")
File "IMAPReader.py", line 22, in strip_tags
s.feed(html)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/HTMLParser.py", line 108, in feed
self.goahead(0)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/HTMLParser.py", line 148, in goahead
k = self.parse_starttag(i)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/HTMLParser.py", line 229, in parse_starttag
endpos = self.check_for_whole_start_tag(i)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/HTMLParser.py", line 304, in check_for_whole_start_tag
self.error("malformed start tag")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/HTMLParser.py", line 115, in error
raise HTMLParseError(message, self.getpos())
HTMLParser.HTMLParseError: malformed start tag, at line 1, column 9

最佳答案

如果您期望获得无效标记,则您不需要 HTML 解析器。查看 BeautifulSoup:

http://www.crummy.com/software/BeautifulSoup/

他们有一个很好的例子,可以准确地做你想做的事情:

http://www.crummy.com/software/BeautifulSoup/bs4/doc/

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>

<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

soup = BeautifulSoup(html_doc)

print(soup.get_text())

返回...

# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
#
# ...

关于python - 将 HTML 解析为纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13390331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com