LoginSignup
0
2

More than 3 years have passed since last update.

【Python tips】htmlタグの削除の方法

Posted at

1.はじめに

今回はテキストデータからHTMLタグを削除する方法を紹介します。
htmlタグは文章の意味を学習させる際にノイズとなるデータです。
なので、削除してあげることが重要になります。

2.環境

  • MacOS Catalina10.15.5
  • Miniconda
  • Python3.7.5

3.例題

はまってしまう!楽しい!! ついついやってしまいます。<br />簡単なものから難しいものまで<br />子供と一緒に楽しく遊べて考えながら<br />出来るので面白いです。

Amazon Reviewのデータセットから
タグが付いている文章を抽出しました。

ここから
タグを抜き出しましょう。

4.方法

4.1.beautiful Soupのインストール

まずはPythonライブラリであるbeautiful soupをMiniconda環境にインストールします。

>conda install beautiful Soup

4.2.変数htmlに文字列を代入

>>>html = ""はまってしまう!楽しい!!   ついついやってしまいます。<br />簡単なものから難しいものまで<br />子供と一緒に楽しく遊べて考えながら<br />出来るので面白いです。""

4.3.HTMLタグを除去する関数を定義

from bs4 import BeautifulSoup

def clean_html(html, strip=False):
      soup = BeautifulSoup(html, 'html.parser')
    text = soup.get_text(strip=strip)
      return text

4.4.出力する

clean_html(html)

他にも正規表現を使った方法などもあるので追々紹介できたらと思います!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2