大きなテキストから文章を抽出したい。私のテキストはtihのようなものです -自由に流れるテキストからhtmlタグを削除して別の文章を作成する
<ul><li>Registered Nurse in <font>Missouri</font>, License number <font>xxxxxxxx</font>, <font>2017</font></li><li>AHA Advanced Cardiac Life Support (ACLS) Certification <font>2016-2018</font></li><li>AHA PALS - Pediatric Advanced Life Support 2017-2019</li><li>AHA Basic Life Support 2016-2018</li></ul>
上記の文章から適切な文章を抽出したいと思います。だから、期待される出力は、私は上記のようなテキストからHTMLSを取り除くためにHTMLParser
モジュール作り付けのpythonを使用し、リスト
['Registered Nurse in Missouri, License number xxxxxxxx, 2017',
'AHA Advanced Cardiac Life Support (ACLS) Certification 2016-2018',
'AHA PALS - Pediatric Advanced Life Support 2017-2019',
'AHA Basic Life Support 2016-2018']
になります。ここに私のコードです。
class HTMLStripper(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.strict = False
self.convert_charrefs= True
self.fed = []
def handle_data(self, chunk):
#import pdb; pdb.set_trace()
self.fed.append(chunk.strip())
def get_data(self):
return [x for x in self.fed if x]
def strip_html_tags(html):
try:
s = HTMLStripper()
s.feed(html)
return s.get_data()
except Exception as e:
# Remove html strings from the given string
p = re.compile(r'<.*?>')
return p.sub('', html)
それのように私は<ul> or <li> tags
に厳格なチェックを行うことはできません(それは現在の実装で生成する必要がありInfactは出力されます)上記のテキスト
['Registered Nurse in', 'Missouri', ', License number', 'xxxxxxx', ',', '2017', 'AHA Advanced Cardiac Life Support (ACLS) Certification', '2016-2018', 'AHA PALS - Pediatric Advanced Life Support 2017-2019', 'AHA Basic Life Support 2016-2018']
にstrip_html_tags
関数を呼び出した上で次のような結果になります異なるテキストは異なるHTMLタグを持つことがあります。私は上記のようなテキストを外側のhtml-tags
に分割するのではなく、毎回分割するよりhtml-tag
が発生しました
ありがとうございます。
'[re.sub(R '<.*?>'、 '' html.splitの行のため、行)( '