6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[python]Beautifulsoupとseleniumを使ってDアニメストアをスクレイピングし、人気アニメランキング作ってみた

Last updated at Posted at 2018-12-02

動機

  • 今世間でどんなアニメが人気なのかを客観視したい

何をやるか

  • アニメ配信サイトとしては最大手であるDアニメストアから全アニメのお気に入り数をスクレイピングで取得し、多いものから順にCSV出力する。

使用技術

  • python3.7.0
  • selenium
    • pip install seleniumでインストール出来ます。
  • Beautifulsoup
    • pip install beautifulsoup4でインストール出来ます。
  • Chrome Driver

実装

anime_scraping.py
from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep
import csv

# リンク文字列
hiragana_list = [
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
]
driver = webdriver.Chrome(executable_path="chromeドライバの保存先パス")
driver.get("https://anime.dmkt-sp.jp/animestore/c_all_pc?initialCollectionKey=1")
results = {}
for hiragana in hiragana_list:
    # 最初のアクセス時以外、五十音順にクリックで遷移
    if hiragana != "":
        continue_link = driver.find_element_by_link_text(hiragana)
        continue_link.click()

    html01 = driver.page_source
    # 最後までページスクロール
    while 1:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        sleep(1)
        html02 = driver.page_source
        if html01 != html02:
            html01 = html02
        else:
            break

    soup = BeautifulSoup(html01, "html.parser")
    titleAndFavoriteCounts = soup.find_all(class_='textContainerIn')

    # {アニメタイトル: ファボ数}
    for titleAndFavoriteCount in titleAndFavoriteCounts:
        title = titleAndFavoriteCount.find(class_='ui-clamp webkit2LineClamp').get_text()
        favoriteCount = int(titleAndFavoriteCount.find(class_='favoriteCount').get_text())
        results[title] = favoriteCount

driver.close()
driver.quit()

sorted_results = {}
# 降順にソート
for k, v in sorted(results.items(), key=lambda x: x[1], reverse=True):
    sorted_results[k] = v

field_names = ['anime_title', 'favorite_count']

# csv出力
with open("anime_ranking.csv", "w") as f:
    writer = csv.writer(f, delimiter=';')

    # ヘッダー
    writer.writerow(field_names)

    # 各行
    for anime_title, favorite_count in sorted_results.items():
        writer.writerow((anime_title, favorite_count))


結果

  • 全アニメ数2657本
  • 以下にお気に入り数top20を示す
スクリーンショット 2018-12-02 20.18.35.png

考察

  • やっぱ最近のアニメは強い。当たり前か。
  • SAOtueeeee。キリトさんさすがっす。
  • ラノベ原作アニメが強い。top20のうち実に14本をラノベ原作アニメが占めることとなった。
  • 逆にオリジナルアニメが一本も無い。やはり原作ものの方が多くの人が触れる機会があるということだろうか。
6
8
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
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?