arcanum_jp’s blog

おっさんの日記

bilibiliのAPIを触ってみる

思うところがありまして、、、bilibiliのAPIを触ってみました。

API自身はまとめているのがbilibiliにありました
www.bilibili.com

ここをみてるとどうやら単純な検索APIはないような感じがするのです。中国語読めないのだけれど・・・そのため一旦サイトの検索レスポンスをスクレイピングするか・・・と。
これですね。検索ボックスから検索するとURLのクエリに反映される。あぁ、URLクエリを編集してGETで叩けばええのすね?楽勝楽勝!

f:id:arcanum_jp:20201231231707p:plain

とまぁ、プログラムからやってみるとわかるのですが、これHTTPステータス403で返ってきます。なるほど、直叩きはダメなのですね・・・もしかしてヘッダーにBeareトークンとかあんのかな?と検索リクエストをみてると・・・おや?なんか別のリクエスト叩いているように見える・・・このURLって・・・さっきのAPIじゃないかな?と確認。

f:id:arcanum_jp:20201231232203p:plain


実際には先のAPIまとめにはないのですが、これを元にクエリのkeywordの部分を任意のワードにすれば良いのかなと。で、やってみたらビンゴでした。以下、実際のJsonの一部

*実際にはkeywordの値はHTMLエンコードしてます
https://api.bilibili.com/x/web-interface/search/all/v2?context=&page=1&order=&duration=&tids_1=&tids_2=&__refresh__=true&_extra=&highlight=1&single_column=0&keyword=洛天依 唱给雅音宫羽

{"code":0,"message":"0","ttl":1,"data":{"seid":"1960205681501073980","page":1,"pagesize":20,"numResults":36,"numPages":2,"suggest_keyword":"","rqt_type":"search","cost_time":{"params_check":"0.000459","illegal_handler":"0.007807","as_response_format":"0.003610","mysql_request":"0.000046","as_request":"0.018292","as_request_format":"0.001309","deserialize_response":"0.000333","total":"0.032297","main_handler":"0.023935"},"exp_list":{"5505":true},"egg_hit":0,"pageinfo":{"live_room":{"numResults":0,"total":0,"pages":0},"pgc":{"numResults":0,"total":0,"pages":0},"operation_card":{"numResults":0,"total":0,"pages":0},"tv":{"numResults":0,"total":0,"pages":0},"movie":{"numResults":0,"total":0,"pages":0},"bili_user":{"numResults":0,"total":0,"pages":0},"live_master":{"numResults":0,"total":0,"pages":0},"live_all":{"numResults":0,"total":0,"pages":0},"topic":{"numResults":0,"total":0,"pages":0},"upuser":{"numResults":0,"total":0,"pages":0},"live":{"numResults":0,"total":0,"pages":0},"video":{"numResults":35,"total":35,"pages":2},"user":{"numResults":0,"total":0,"pages":0},"bangumi":{"numResults":0,"total":0,"pages":0},"activity":{"numResults":0,"total":0,"pages":0},"media_ft":{"numResults":0,"total":0,"pages":0},"article":{"numResults":1,"total":1,"pages":1},"media_bangumi":{"numResults":0,"total":0,"pages":0},"special":{"numResults":0,"total":0,"pages":0},"live_user":{"numResults":0,"total":0,"pages":0}},"top_tlist":{"live_room":0,"pgc":0,"operation_card":0,"tv":0,"movie":0,"special":0,"live_master":0,"bili_user":0,"topic":0,"upuser":0,"live":0,"video":35,"user":0,"bangumi":0,"activity":0,"media_ft":0,"article":1,"media_bangumi":0,"card":0,"live_user":0},"show_column":0,"show_module_list":["activity","web_game","card","media_bangumi","media_ft","bili_user","user","star","video"],"result":[{"result_type":"activity","data":[]},{"result_type":"web_game","data":[]},{"result_type":"card","data":[]},{"result_type":"media_bangumi","data":[]},{"result_type":"media_ft","data":[]},{"result_type":"bili_user","data":[]},{"result_type":"user","data":[]},{"result_type":"star","data":[]},{"result_type":"video","data":[{"type":"video","id":333069,"author":"純白","mid":123372,"typeid":"30","typename":"VOCALOID·UTAU","arcurl":"http://www.bilibili.com/video/av333069","aid":333069,"bvid":"BV14x411F7np","title":"【\u003cem class=\"keyword\"\u003e洛天依\u003c/em\u003e】\u003cem class=\"keyword\"\u003e唱给雅音宫羽\u003c/em\u003e【原创曲PV付】","description":"作词:薇采之依 作曲:纯白 编曲:纯白\u0026amp;amp;Jerrkiy 调教:Commando1945 混音:Axiom.Latis 插图:Circus 黑炎-M82A1 崩坏7 盐 这首歌描述了一个凄美的爱情故事(这难道是百合系神曲...),紀念遠在天國的雅音菌的一首歌。 洛天依唱给雅音宫羽的离别之曲,送别雅音宫羽。 捂脸社出品。 (順便說一下,PV上的畫師名寫錯了,這裡給黑炎大大道歉了。)","arcrank":"0","pic":"//i1.hdslb.com/bfs/archive/fae0ab44e4b0b87f3613cd1a12a8085c80156eb8.jpg","play":388775,"video_review":9034,"favorites":11522,"tag":"原创,PV,良曲,催泪,捂脸社,初代人设,洛天依,洛天依原创曲,雅音宫羽","review":2974,"pubdate":1344733966,"senddate":1598516157,"duration":"10:38","badgepay":false,"hit_columns":["title","description","tag"],"view_type":"","is_pay":0,"is_union_video":0,"rec_tags":null,"new_rec_tags":[],"rank_score":111323314},{"type":"video","id":1773542,"author":"純白","mid":123372,"typeid":"30","typename":"VOCALOID·UTAU","arcurl":"http://www.bilibili.com/video/av1773542","aid":1773542,"bvid":"BV1Tx411N7H6","title":"【\u003cem class=\"keyword\"\u003e洛天依\u003c/em\u003e】\u003cem class=\"keyword\"\u003e唱给雅音宮羽\u003c/em\u003eⅡ【原创曲PV付】【Sodatune】","description":"自制 《唱给雅音宮羽Ⅰ》→av333069 曲:纯白 词:薇采之依 图:盐 因为特殊原因,把PV换成一图流版・・・以下略


んで、このJsonでかすぎて分からんかったのですが、どうやら属性が"result"に配列で各種データが入っている模様。今回はビデオが欲しいのでその中で"result_type": "video"の属性があるJsonのdataに検索された複数のビデオ情報が入っています。

result: [
    ...
    {"result_type": "star", "data": []},               <-- 他の結果
    {
        "result_type": "video" ,
        "data": [
            {"type": "video", "id": NNNNNN1, },    <-- 曲データ1
            {"type": "video", "id": NNNNNN2, },    <-- 曲データ2
            {"type": "video", "id": NNNNNN3, },    <-- 曲データ3
            ... 略
        ]
]


とここまでやって以下でまとめている方がいらっしゃいました。
github.com

今回自分が上記でやった検索APIの場合はこちらですね。
github.com




ちゃんちゃん