1

いくつかの投稿で推奨されている MLStripper クラスを使用して、プレーンテキストを取得するために電子メールから html を削除しようとしています。「@」記号が原因で解析しようとすると、strip_tags 関数で問題が発生します。このクラスは、有効な html タグのみを解析するほど強力ではないと思います。以下を修正して「@」または別のライブラリを処理してテキストから html を削除する方法に関する推奨事項はありますか? & のようなものも削除する必要があります。

パイソン:

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
4

2 に答える 2

3

無効なマークアップが予想される場合は、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.
#
# ...
于 2012-11-15T02:25:29.853 に答える
0

Python のどのバージョンを使用していますか? Python 2.7.2 を使用してコードを実行したところ、同じエラーが発生しました。その後、Python 2.7.3を搭載したコンピューターで実行したところ、完全に動作しました。これはかなり奇妙だったので調べてみたところ、Python に含まれる HTML パーサーが後のバージョンでより寛大になったと書かれているドキュメントが見つかりました。2.7.3 にアップグレードしてみてください。動作するはずです。

于 2012-11-15T02:41:48.803 に答える