概要
化粧サイトLIPSでは化粧品のランキングが見れます。
今回はアイシャドウのランキングを取得し、
縦軸は商品の名前、横軸を星の評価にしてグラフにしたいと思います。
上から下への流れが順位となります。
完成
コード
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
# LIPSのサイトをスクレイピング
urlName = "https://lipscosme.com/"
url = requests.get(urlName)
url.raise_for_status()
bs = BeautifulSoup(url.text, "html.parser")
url_list = []
# url_listにアイシャドウ、ファンデーション、口紅、化粧水のURLが入っている
# 今回はアイシャドウのみをグラフにする
for i in bs.select('a.ranking-products-list__more-link'):
url_list.append(urlName + i.get('href'))
url_list.pop() # 化粧水のURLを削除
url_list.pop() # 口紅のURLを削除
url_list.pop() # ファンデーションのURLを削除
name_list = []
start_list = []
for i in url_list:
url_temp = requests.get(i)
url_temp.raise_for_status()
bs_temp = BeautifulSoup(url_temp.text, "html.parser")
for j in bs_temp.select('div.ProductListArticle'):
for k in j.select('div.ProductListArticle__product'):
for k2 in k.select('h2.ProductListArticle__productTitle-productName'):
name_list.append(k2.text)
for k3 in k.select('span.ratingStar__num'):
start_list.append(k3.text)
# グラフ作成
# 上位10位
start_list = start_list[:10]
name_list = name_list[:10]
# 上から下にかけて順位を表示させるため、逆順にする
start_list.reverse()
name_list.reverse()
labels = [float(i) for i in start_list] # 横のラベルに星の評価を入れる
height = name_list # 縦のラベルにアイシャドウの名前を入れる
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.barh(height,labels,height = 0.5)
plt.xticks(rotation=90)
plt.title('アイシャドウのランキング10選', fontsize=15)
plt.ylabel("化粧品名", fontsize=15)
plt.xlabel("星の評価", fontsize=15)
plt.tick_params(labelsize=10)
plt.show()
まとめ
ランキング1位から下に行くほどに星の評価が下がると思いきや
余り星が変わらない結果となりました。
アイシャドウ買う時はこのグラフを参考にしてもらえると嬉しいです!
次回はファンデーションとか他の化粧品のグラフを作ってみよう。