patchgi
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # モンストのキャラクターのプロフィールが知りたい [ミクシィ20新卒AdventCalendar2019](https://adventar.org/calendars/4207) 6日目担当の[@patchgi](https://twitter.com/patchgi)です. ←前日 [死ぬまでにやりたいこと](https://hackmd.io/@takunoko/B1Ij7F-nH) 静岡出身だけど, 富士山登ったことないなぁ... **富士山は静岡のものだけどね** 翌日→[人工知能でロウソク足予測](https://qiita.com/july1997/items/33ab007aea5f28c411c2) 今回はモンスト好きな筆者が苦手なプログラミングを片手に 自分の趣味に没頭したお話です. 記事内容の9割が箇条書き + 拙い文章力で書かれているんで読みにくかったらすみません ## 自己紹介 - 都内の大学院の研究室に所属する男 - モンストが好き ![](https://i.imgur.com/VYcsKwv.jpg) ## 概要 モンストのキャラクターの誕生日を通知する君を作るために辿った道筋 ## モチベーション よりモンストの世界を身近に感じたいという願望 + AdventCalendarを途切らせてはならないという使命感 ## やったこと - [XFLAG DICTIONARY](https://dic.xflag.com/monsterstrike/)からモンスターのプロフィールをスクレイピング - 普通にXFLAG DICTIONARYを使えよ!って思う人もいるかもだけど, 名前でしか検索できないし, 一覧性が低いし, CSVとかを通してみたかっし, オタクだから!!それだけ!!! ## XFLAG DICTIONARY とは > モンスターストライク(モンスト)の公式モンスタープロフィール特設サイト。ゲーム内では語られないキャラクター達の一面や関係性など様々な情報掲載中!(XFLAG DICTIONARYより) モンストに出てくるキャラクターの簡単なプロフィールがまとまっているサイト(公式) [XFLAG DICTIONARY](https://dic.xflag.com/monsterstrike/) ![](https://i.imgur.com/N4a7QLs.png) ## 調査編 XFLAG DICTIONARYがスクレイピング可能なサイトかを調べる. ### 調査項目 - robots.txt - クローラに対してどのURLにアクセスして良いか、してはいけないかが書いてある. - **やったこと** - robots.txtを目視で確認 - 一応昔書いたrobots.txtのパーサを使って確認 - https://gist.github.com/patchgi/dc58c20d17cca7d40b23db1018e06292 - JSで書かれているサイトか - HTMLやCSSで書かれているサイトであれば, サイトがホストしてる静的なファイルを適当にパースすればいい感じにスクレイピングできる(簡単!!) - **But** そうでなかった場合, スクレイピングする前にサイトのJSを事前に実行しなくてはならない. - **やったこと** - XFLAG DICTIONARYでJSを切った状態でページの状態を確認 - JS OFF(ChromeApp) - [ChromeWebStore](https://chrome.google.com/webstore/detail/jsoff/kjhbibcocglfnpllfodaiabanmmegomm) - 必要な情報(キャラクターのプロフィール)を取得するためにどんな情報が欲しいか - **やったこと** - 実際にサイトに行ってDeveloperToolとにらめっこ ### 結果 - [robots.txt](https://dic.xflag.com/robots.txt) - **result: 多分大丈夫...** - JSで書かれているサイトか - JSあり - ![](https://i.imgur.com/bJIX6ai.png) - JSなし - ![](https://i.imgur.com/6wuZpiG.png) - **result: JSで書かれてた...** - 必要な情報(キャラクターのプロフィール)を取得するためにどんな情報が欲しいか - 各キャラクターのプロフィールを取得するには, そのキャラクターのプロフィールページのリンクを取得する必要がある. ``` https://dic.xflag.com/monsterstrike/character/{キャラクターのID}``` - キャラクターのIDが分かれば, 各キャラクターのプロフィールページのリンクがわかる. このIDが連番になっていれば楽だったが,そうは問屋が卸さなかったのでこのIDのリストをスクレイピングで取得する. - 各キャラクターのIDは, キャラクターリストページの各キャラクターの画像のファイル名と同じ & 他にそれっぽい情報がなかったから, 画像のファイル名からキャラクターのIDを抽出する. - **result: キャラクターのリストページから各キャラクターの画像のファイル名を取得し, 紐づけられたIDを抽出する必要がある** ## 構想編 ### 実装項目 - スクレイピング - ページをJSを実行された状態でDOMを取りたい - headless chromeとかPhantomJSが扱える言語なら何でもいい - データ保存 - DBで管理するまでもないけど, いい感じに確認とかしたい - CSV(GoogleSpreadSheet)に保存 GoogleSpreadSheetを簡単に操作するっていうと,GoogleAppScript(GAS)が一番最初に頭に浮かんだから, 言語は**GAS**にした。でも, GASってheadlessChromeとかを扱えるライブラリとかってあるんだろうか?って思って調べたらライブラリではないけどぽいのがあった[PhantomJSCloud](https://phantomjscloud.com/index.html). ページのURLを投げると, JSを実行した状態のHTMLtxtを返してくれるサービスらしい. 調べた感じ, 1日500pageまでなら無料で使えるらしい. XFLAG DICTIONARYに登録されているキャラクターは現在(2019/12/04)300未満なため, 運用するなら問題なさそう.(デバッグするなら別だけど)これを使おうと思う. 余談だけど, PhantomJSって随分前にメンテが終了した気がしたけど, こういう形で残っていて感慨深かった. ### 構想まとめ - 言語: GAS - JS実行: PhantomJSCloudのAPIを利用 - データ管理: CSV(GoogleSpreadSheet) GASを採用した理由は他にも, 「実行環境を準備しなくてもいい.」 「通知をするための定期実行の仕組みがデフォでサポートされてて楽そう」ってのがある. ## 実装編 https://gist.github.com/patchgi/847b931c6bc4f16b1b28ce70f0ada246 ↑最終的に書いたコード ![](https://i.imgur.com/WECm9n0.png) ↑取ってきたプロフィールデータ一部 ### 実装システムの流れ 1. キャラクターリストページをPhantomJSCloudに投げ, JSが実行された状態でのHTMLtxtを取得 2. 返ってきたHTMLtxtをパースし, 各キャラクターに紐づけられたIDのリストを取得 3. 作ったIDリストを元に, 各キャラクターのプロフィールページのリンクを作成し, 再びPhantomJSCloudに投げJSが実行された状態でのHTMLtxtを取得 4. 返ってきたHTMLtxtをパースし, プロフィール情報を抽出 5. 抽出したプロフィール情報をGoogleSpreadSheetに書き込み 今回はスクレイピングまで...通知とかは後日やる ### 実装を終えて #### Keep - GoogleSpreadSheetの扱いはめちゃ楽だった. - 環境構築をする必要がなかった. - GAS自体あまり触ったことなかったけど, ほぼJavaScriptだからそんなにつまづくことがなかった. #### Problem - GASではES6の記法が使えなかった... - ES6でかけるようにしてる人もいたけど, 面倒くさそうだったし, そこまでやったら環境構築楽っていうメリットがなさそう... っていう謎のプライドが発生してやらなかった. - やればよかった - GASはJavaScriptっぽいくせに, DOMのパーサがないから, 正規表現とかを使って要素を抽出することになった... - (正確にはパーサのライブラリあったけど, うまく動かなかった) - headless browserの部分を外部のサービスに委託したから, 1リクエストに結構時間がかかる(1リクエスト5 ~ 10秒くらい) - インフラとかそこらへんに対する知識一切ないけど, リクエストに時間がかかるおかげで, サイト(XFLAG DICTIONARY)へはそんなに負荷はかかってないんじゃないかなぁ... - リクエストに時間がかかるせいで, GASの1回の実行時間上限を超えてしまう. - 何回かに分けて実行した #### Try - 使い慣れてる言語でやる. - 「CSV扱いたい」とか「環境構築が楽」って理由だけで, GASを採用したけど, よくよく考えるとCSVならGoogleSpreadSheetとかに拘る必要ないし, PythonとかRubyとか使い慣れてる言語でやればよかったなぁ... - でも, 普段使わないツールを使うのはそこそこ楽しかった - それにしてもGASを使うのはセンスがなかった感もある... ## 終わりに - 目的のデータを取ってくることができてよかった - あとは, 取ってきたデータを煮るなり焼くなり好きに遊ぶだけ! - 誕生日の表記が統一されていないので, そこは気をつけたい - 例 - ![](https://i.imgur.com/rAFM3ec.png) - ![](https://i.imgur.com/b2GCQgb.png) - 今回は進化前のプロフィールのみを取得したけど, 一部進化するとプロフィールが変わるキャラクターがいるのでそこも取りたい - 後日このデータをどう使ったかとかの記事を書くかも... - こういうWebスクレイピングの記事をあげるの少し怖いんだけど, そんなに負荷をかけているわけでもないし, 営利目的で収集しているわけでもないし, 会員限定の情報とかを抜いてるわけでもないし, 大丈夫だとは思っている... [Webスクレイピングの法律周りの話をしよう!](https://qiita.com/nezuq/items/3cc9772118ad112c18dc) [Webスクレイピングの注意事項一覧](https://qiita.com/nezuq/items/c5e827e1827e7cb29011) - **問題がありそうだったらこの記事取り下げます** 以上, モンストのキャラクターの誕生日を知りたい男の辿った道筋でしたー (ジル・ド・レと誕生日が一緒だったから今度運極作ろうと思う.) - ![ジル・ド・レ](https://i.imgur.com/7VKcmAZ.png) ジル・ド・レ

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully