【Python】ワードクラウドを使ってサイトの単語の使用頻度を見える化する


目的

指定のサイトで使用頻度が高い単語をワードクラウドを使って視覚化したい

コード

import requests
from bs4 import BeautifulSoup
import MeCab
from wordcloud import WordCloud

# ワードクラウドの保存ファイル名
file_name = "hage.png"

# ワードクラウド化の対象サイトのURL
url = 'https://ja.wikipedia.org/wiki/%E3%83%8F%E3%82%B2'

# 対象サイトのURLのページを取得して解析
html = requests.get(url)
soup = BeautifulSoup(html.content, 'html.parser')

# scriptとstyleを要素から除去
for element in soup(["script", "style"]):
    element.decompose()

# テキストを抽出
text = soup.get_text()
# 行ごとに分けて両端の空白文字を削除
lines = [line.strip() for line in text.splitlines()]
# 改行文字を結合
text = "\n".join(line for line in lines if line)

# 形態素解析する
mecab = MeCab.Tagger("-Ochasen")
node = mecab.parseToNode(text)

word_list = []
# 品詞を使って単語を分ける
while node:
    # 品詞
    part = node.feature.split(",")[0]
    # 配列の追加する単語を品詞で指定する
    if part in ["動詞","副詞","形容詞","名詞"]:
        word = node.surface # 単語
        word_list.append(word)
    node = node.next

words = ' '.join(word_list)

# ワードクラウドで使用するフォント(メイリオ)のパス
fpath = "C:/Windows/Fonts/meiryo.ttc"

# ワードクラウドの作成
wordcloud = WordCloud(background_color="white",
                     font_path=fpath,
                     width=800,
                     height=600).generate(words)

# 指定の名前で保存する
wordcloud.to_file("./"+file_name)