{"id":86,"date":"2024-04-15T02:07:36","date_gmt":"2024-04-14T18:07:36","guid":{"rendered":"https:\/\/www.nickchan.cn\/?p=86"},"modified":"2024-07-11T02:32:27","modified_gmt":"2024-07-10T18:32:27","slug":"bilibili-com-%e8%a7%86%e9%a2%91%e5%b0%81%e9%9d%a2%e7%88%ac%e5%8f%96","status":"publish","type":"post","link":"https:\/\/www.nickchan.cn\/index.php\/2024\/04\/15\/bilibili-com-%e8%a7%86%e9%a2%91%e5%b0%81%e9%9d%a2%e7%88%ac%e5%8f%96\/","title":{"rendered":"Bilibili.com \u89c6\u9891\u5c01\u9762\u722c\u53d6"},"content":{"rendered":"<h1>\u529f\u80fd\uff1a<\/h1>\n<p>\u5b9e\u73b0\u6307\u5b9aB\u7ad9UID\u4f5c\u8005\u6240\u6709\u89c6\u9891\u5c01\u9762\u722c\u53d6\uff0c\u5e76\u4fdd\u5b58\u5230\u672c\u5730folder\u4e2d<\/p>\n<h1>\u4ee3\u7801\uff1a<\/h1>\n<pre><code class=\"language-python\">from functools import reduce\nimport hashlib\nimport os\nimport time\nimport http.cookies\nimport time\nimport requests\nimport urllib.parse\n\nx = 0\n\ndef parse_cookies(cookie_str):\n    cookies = {}\n    if cookie_str:\n        cookie = http.cookies.SimpleCookie(cookie_str)\n        for key, morsel in cookie.items():\n            cookies[key] = morsel.value\n    return cookies\n\ndef getMixinKey(ae):\n    oe = [46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41,\n          13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, 36, 20, 34, 44, 52]\n    le = reduce(lambda s, i: s + ae[i], oe, &quot;&quot;)\n    return le[:32]\n\ndef getjson(url,headers=None):\n    cookie = &quot;&quot;\n    cookies = parse_cookies(cookie)\n    headers = {&#039;Referer&#039;: &#039;https:\/\/www.bilibili.com\/&#039;,&#039;User-Agent&#039;: &#039;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/120.0.0.0 Safari\/537.36 Edg\/120.0.0.0&#039;}\n    res = requests.get(url,headers=headers, cookies=cookies)\n    if res.status_code == 200:\n        json_data = res.json()\n        return json_data\n    else:\n        return None\n\ndef encWbi(params_in: dict): \n    params = params_in.copy() \n    print(f&#039;params: {params}&#039;)  # \u8f93\u51faparams\u7684\u503c\n    resp = getjson(&quot;https:\/\/api.bilibili.com\/x\/web-interface\/nav&quot;)\n    print(f&#039;resp: {resp}&#039;)  # \u8f93\u51faresp\u7684\u503c\n    wbi_img: dict = resp[&quot;data&quot;][&quot;wbi_img&quot;]\n    print(f&#039;wbi_img: {wbi_img}&#039;)  # \u8f93\u51fawbi_img\u7684\u503c\n    me = getMixinKey(wbi_img[&#039;img_url&#039;].split(&quot;\/&quot;)[-1].split(&quot;.&quot;)[0] + wbi_img[&#039;sub_url&#039;].split(&quot;\/&quot;)[-1].split(&quot;.&quot;)[0])\n    print(f&#039;me: {me}&#039;)  # \u8f93\u51fame\u7684\u503c\n    wts = int(time.time())\n    print(f&#039;wts: {wts}&#039;)  # \u8f93\u51fawts\u7684\u503c\n    params[&quot;wts&quot;] = wts\n    print(f&#039;params after adding wts: {params}&#039;)  # \u8f93\u51fa\u6dfb\u52a0wts\u540e\u7684params\u7684\u503c\n    params = dict(sorted(params.items()))\n    print(f&#039;params after sorting: {params}&#039;)  # \u8f93\u51fa\u6392\u5e8f\u540e\u7684params\u7684\u503c\n    Ae = &quot;&amp;&quot;.join([f&#039;{key}={value}&#039; for key, value in params.items()])\n    print(f&#039;Ae: {Ae}&#039;)  # \u8f93\u51faAe\u7684\u503c\n    w_rid = hashlib.md5((Ae + me).encode(encoding=&#039;utf-8&#039;)).hexdigest()\n    print(f&#039;w_rid: {w_rid}&#039;)  # \u8f93\u51faw_rid\u7684\u503c\n    return w_rid, wts\n\n# \u8f93\u5165uid \u8fd4\u56de\u6295\u7a3f\u89c6\u9891\u7684\u5b57\u5178\u5217\u8868\ndef getUpVideos(up_uid,startpage=1,endpage=10,tid=0,keyword=&#039;&#039;):\n    up_videos = []\n    for space_video_page in range(startpage,endpage+1): #\u6700\u591a\u4e0b\u8f7d10\u9875 300\u4e2a\u89c6\u9891\n        time.sleep(3) # \u9891\u7387\u4e0d\u5b9c\u8fc7\u5feb\n        space_video_search_params_dict={&#039;host_mid&#039; : up_uid, # UP\u4e3bUID\n                                        &#039;page&#039;  : space_video_page,      # \u9875\u7801\n                                        &#039;web_location&#039;:333.999,\n                                        }\n        w_rid, wts = encWbi(space_video_search_params_dict)\n        space_video_search_params_urlcoded = urllib.parse.urlencode(space_video_search_params_dict)\n        up_videos_api = &#039;https:\/\/api.bilibili.com\/x\/space\/wbi\/arc\/search?%s&amp;w_rid=%s&amp;wts=%s&#039;%(space_video_search_params_urlcoded,w_rid,wts)\n        space_video_search_json = getjson(up_videos_api,headers=[(&quot;credentials&quot;,&quot;include&quot;)])\n        if space_video_page == startpage:\n            #\u83b7\u53d6\u5206\u7c7b\u8868 \u5982\u679c\u8be5\u9875\u65e0\u89c6\u9891\u5219\u8fd4\u56deNone\n            # tlist = space_video_search_json[&#039;data&#039;][&#039;list&#039;][&#039;tlist&#039;]\n            # for each in tlist :\n            #     print(&#039;tid:&#039;,tlist[each][&#039;tid&#039;],&#039;\u7c7b\u540d:&#039;,tlist[each][&#039;name&#039;],&#039;\u6570\u76ee:&#039;,tlist[each][&#039;count&#039;])\n\n            #\u83b7\u53d6\u89c6\u9891\u603b\u6570 \u5982\u679c\u8be5\u9875\u65e0\u89c6\u9891\u5219\u8fd4\u56de0\n            space_video_num = space_video_search_json[&#039;data&#039;][&#039;page&#039;][&#039;count&#039;]\n\n        if space_video_search_json[&#039;data&#039;][&#039;list&#039;][&#039;vlist&#039;]: #\u5982\u679c\u4e0d\u5b58\u5728\u89c6\u9891\u5219\u4e3a\u7a7a\u5217\u8868[]\n            thisPageVideos = space_video_search_json[&#039;data&#039;][&#039;list&#039;][&#039;vlist&#039;]\n            thisPageVideos.reverse()\n            thisPageVideos_num = len(thisPageVideos)\n            for each_video_id in range(thisPageVideos_num):\n                each_video_info = thisPageVideos[thisPageVideos_num-each_video_id-1]\n                # up_videos\u683c\u5f0f\n                up_videos.append({&#039;title&#039;:each_video_info[&#039;title&#039;],\n                                  &#039;bvid&#039;:each_video_info[&#039;bvid&#039;],\n                                  &#039;author&#039;:each_video_info[&#039;author&#039;],\n                                  &#039;mid&#039;:each_video_info[&#039;mid&#039;],\n                                  &#039;created&#039;:each_video_info[&#039;created&#039;],\n                                  })\n\n            if space_video_page == endpage:\n                print(&#039;[\u221a] \u5df2\u83b7\u53d6 [%d\/%d] \u4e2a\u89c6\u9891&#039;%(len(up_videos),space_video_num))\n                return up_videos \n        else:#\u8fd9\u9875\u4e0d\u5b58\u5728\u89c6\u9891\n            print(&#039;[\u221a] \u5df2\u83b7\u53d6 [%d\/%d] \u4e2a\u89c6\u9891&#039;%(len(up_videos),space_video_num))\n            return up_videos\n\ndef download_img(url,name):\n    global x\n    cur_path = os.path.abspath(os.curdir)\n    cookie = &quot;&quot;\n    cookies = parse_cookies(cookie)\n    headers = {&#039;Referer&#039;: &#039;https:\/\/www.bilibili.com\/&#039;,&#039;User-Agent&#039;: &#039;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/120.0.0.0 Safari\/537.36 Edg\/120.0.0.0&#039;}\n    res = requests.get(url,headers=headers,cookies=cookies,timeout=(5,5))\n    if os.path.exists(&quot;D:\\\\B\u7ad9\u5c01\u9762&quot;) == False :\n        os.mkdir(&quot;D:\\\\B\u7ad9\u5c01\u9762&quot;)\n        print(&quot;\u65b0\u5efa\u6587\u4ef6\u5939B\u7ad9\u5c01\u9762\u6210\u529f&quot;)\n    if os.path.exists(&quot;D:\\\\B\u7ad9\u5c01\u9762\\\\&quot;+name+&quot;\u5c01\u9762\u5408\u96c6&quot;) == False :\n        os.mkdir(&#039;D:\\\\B\u7ad9\u5c01\u9762\\\\&#039;+name+&#039;\u5c01\u9762\u5408\u96c6&#039;)\n        print(&quot;\u65b0\u5efa\u6587\u4ef6\u5939&quot;+name+&quot;\u5c01\u9762\u5408\u96c6&quot;+&quot;\u6210\u529f&quot;)\n    for pic in res.json()[&#039;data&#039;][&#039;items&#039;]:\n        path = &quot;D:\\\\B\u7ad9\u5c01\u9762\\\\&quot;+name+&quot;\u5c01\u9762\u5408\u96c6&quot;\n        print(pic[&#039;cover&#039;][&#039;url&#039;])\n        urllib.request.urlretrieve(pic[&#039;cover&#039;][&#039;url&#039;], path+&#039;\\%s.jpg&#039;%x)\n        print(&quot;Downloading image No.{}&quot;.format(x))\n        x += 1\n\n# \u8f93\u5165uid \u8fd4\u56de\u6295\u7a3f\u89c6\u9891\u7684\u5b57\u5178\u5217\u8868\ndef get_images(up_uid,startpage=1,endpage=10):\n    for space_video_page in range(startpage,endpage+1): #\u6700\u591a\u4e0b\u8f7d10\u9875 300\u4e2a\u89c6\u9891\n        time.sleep(3) # \u9891\u7387\u4e0d\u5b9c\u8fc7\u5feb\n        space_video_search_params_dict={&#039;host_mid&#039; : up_uid, # UP\u4e3bUID\n                                        &#039;page&#039;  : space_video_page,      # \u9875\u7801\n                                        &#039;web_location&#039;:333.999,\n                                        }\n        w_rid, wts =encWbi(space_video_search_params_dict)\n        space_video_search_params_urlcoded = urllib.parse.urlencode(space_video_search_params_dict)\n        up_videos_api = &#039;https:\/\/api.bilibili.com\/x\/polymer\/web-dynamic\/v1\/opus\/feed\/space?%s&amp;w_rid=%s&amp;wts=%s&#039;%(space_video_search_params_urlcoded,w_rid,wts)\n        download_img(up_videos_api,up_uid)\n\n#space_video_search_params_dict={&#039;host_mid&#039; : 10835521, # UP\u4e3bUID\n#                                        &#039;page&#039;  : 1,      # \u9875\u7801\n#                                        &#039;web_location&#039;:333.999,\n#                              }\n#w_rid, wts =encWbi(space_video_search_params_dict)\n#space_video_search_params_urlcoded = urllib.parse.urlencode(space_video_search_params_dict)\n#up_videos_api = &#039;https:\/\/api.bilibili.com\/x\/polymer\/web-dynamic\/v1\/opus\/feed\/space?%s&amp;w_rid=%s&amp;wts=%s&#039;%(space_video_search_params_urlcoded,w_rid,wts)\nget_images(&#039;3816626&#039;)\n#print(getUpVideos(3816626))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u529f\u80fd\uff1a \u5b9e\u73b0\u6307\u5b9aB\u7ad9UID\u4f5c\u8005\u6240\u6709\u89c6\u9891\u5c01\u9762\u722c\u53d6\uff0c\u5e76\u4fdd\u5b58\u5230\u672c\u5730folder\u4e2d \u4ee3\u7801\uff1a from functool &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.nickchan.cn\/index.php\/2024\/04\/15\/bilibili-com-%e8%a7%86%e9%a2%91%e5%b0%81%e9%9d%a2%e7%88%ac%e5%8f%96\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cBilibili.com \u89c6\u9891\u5c01\u9762\u722c\u53d6\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[138],"tags":[],"_links":{"self":[{"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/posts\/86"}],"collection":[{"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":3,"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/118"}],"wp:attachment":[{"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nickchan.cn\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}