{"id":1068,"date":"2025-09-07T01:40:38","date_gmt":"2025-09-06T17:40:38","guid":{"rendered":"https:\/\/flighhai.cn\/?p=1068"},"modified":"2025-09-07T01:46:54","modified_gmt":"2025-09-06T17:46:54","slug":"%e7%bd%91%e9%a1%b5%e5%90%ac%e6%ad%8c%e7%9a%84%e5%8d%95%e6%9b%b2%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/flighhai.cn\/?p=1068","title":{"rendered":"\u7f51\u9875\u542c\u6b4c\u7684\u5355\u66f2\u5faa\u73af"},"content":{"rendered":"<div><strong># \u5176\u5b9e\u5c31\u662f\u4e0b\u8f7d\u5230\u672c\u5730\u54c8\u54c8<\/strong><\/div>\n<div><\/div>\n<div>import requests<\/div>\n<div>import re<\/div>\n<div>import json<\/div>\n<div># \u66f4\u6539\u4f60\u8981\u7684url\u548c\u4f60\u81ea\u5df1\u7684cookie<\/div>\n<div>url = &#8216;https:\/\/www.bilibili.com\/video\/BV1kC4y197tf\/&#8217;<\/div>\n<div>cookie = &#8220;&#8221;<\/div>\n<div>headers = {<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 &#8220;Referer&#8221;: &#8220;https:\/\/www.bilibili.com\/video\/BV1kC4y197tf\/&#8221;,<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 &#8220;User-Agent&#8221;: &#8220;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/138.0.0.0 Safari\/537.36&#8221;,<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 &#8220;Cookie&#8221;: cookie<\/div>\n<div>}<\/div>\n<div># \u53d1\u9001\u8bf7\u6c42<\/div>\n<div>response = requests.get(url=url, headers=headers)<\/div>\n<div>html = response.text<\/div>\n<div># \u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/div>\n<div># \u5c06HTML\u4fdd\u5b58\u5230\u5f53\u524d\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u7528 Prettier \u683c\u5f0f\u5316\u4ee3\u7801\u65b9\u4fbf\u5206\u6790<\/div>\n<div>if response.status_code == 200:<\/div>\n<div>\u00a0 \u00a0 with open(&#8216;output.html&#8217;, &#8216;w&#8217;, encoding=&#8217;utf-8&#8242;) as file:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 file.write(response.text)<\/div>\n<div>\u00a0 \u00a0 print(&#8220;HTML\u5df2\u4fdd\u5b58&#8221;)<\/div>\n<div>else:<\/div>\n<div>\u00a0 \u00a0 print(f&#8221;\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a{response.status_code}&#8221;)<\/div>\n<div># \u63d0\u53d6\u89c6\u9891\u6807\u9898<\/div>\n<div>title = re.findall(&#8216;title=&#8221;(.*?)&#8221;&#8216;, html)[0]<\/div>\n<div># \u63d0\u53d6\u89c6\u9891\u4fe1\u606f<\/div>\n<div>info = re.findall(&#8216;window.__playinfo__=(.*?)&lt;\/script&gt;&#8217;, html)[0]<\/div>\n<div># print(type(info))<\/div>\n<div># \u8fd4\u56de\u4e00\u4e2a Python \u5bf9\u8c61\uff0c\u5177\u4f53\u7c7b\u578b\u53d6\u51b3\u4e8e info \u4e2d JSON \u6570\u636e\u7684\u7ed3\u6784<\/div>\n<div># \u5982\u679c JSON \u662f\u4e00\u4e2a\u5bf9\u8c61\uff08\u5373\u7531 {} \u5305\u56f4\uff09\uff0c\u5b83\u4f1a\u8f6c\u6362\u4e3a\u4e00\u4e2a \u5b57\u5178<\/div>\n<div># \u5982\u679c JSON \u662f\u4e00\u4e2a\u6570\u7ec4\uff08\u5373\u7531 [] \u5305\u56f4\uff09\uff0c\u5b83\u4f1a\u8f6c\u6362\u4e3a\u4e00\u4e2a \u5217\u8868<\/div>\n<div># str -&gt; dic<\/div>\n<div>json_data = json.loads(info)<\/div>\n<div>with open(&#8220;data.json&#8221;, &#8220;w&#8221;, encoding=&#8221;utf-8&#8243;) as file:<\/div>\n<div>\u00a0 \u00a0 json.dump(json_data, file, ensure_ascii=False, indent=4)<\/div>\n<div># print(type(json_data))<\/div>\n<div>audio_data = json_data[&#8220;data&#8221;][&#8220;dash&#8221;][&#8220;audio&#8221;]<\/div>\n<div>video_data = json_data[&#8220;data&#8221;][&#8220;dash&#8221;][&#8220;video&#8221;]<\/div>\n<div>def get_max_bandwidth_url(input_data):<\/div>\n<div>\u00a0 \u00a0 max_bandwidth = -1<\/div>\n<div>\u00a0 \u00a0 max_base_url = &#8220;&#8221;<\/div>\n<div>\u00a0 \u00a0 if input_data:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 for input in input_data:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 bandwidth = input[&#8216;bandwidth&#8217;]<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if bandwidth &gt; max_bandwidth:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 max_bandwidth = bandwidth<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 max_base_url = input[&#8216;baseUrl&#8217;]<\/div>\n<div>\u00a0 \u00a0 if max_base_url:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 return max_base_url<\/div>\n<div>\u00a0 \u00a0 else:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 print(f&#8221;No baseUrl found in this {input_data}&#8221;)<\/div>\n<div>audio_url = get_max_bandwidth_url(audio_data)<\/div>\n<div>print(&#8220;Audio Url:&#8221;, audio_url)<\/div>\n<div>video_url = get_max_bandwidth_url(video_data)<\/div>\n<div>print(&#8220;Video Url:&#8221;, video_url)<\/div>\n<div># \u4fdd\u5b58\u6570\u636e<\/div>\n<div>audio_content = requests.get(url=audio_url, headers=headers).content<\/div>\n<div>with open(title + &#8216;.mp3&#8242;, mode=&#8217;wb&#8217;) as a:<\/div>\n<div>\u00a0 \u00a0 a.write(audio_content)<\/div>\n<div># video_content = requests.get(url=video_url, headers=headers).content<\/div>\n<div># with open(title + &#8216;.mp4&#8242;, mode=&#8217;wb&#8217;) as v:<\/div>\n<div># \u00a0 \u00a0 v.write(video_content)<\/div>\n","protected":false},"excerpt":{"rendered":"<p># \u5176\u5b9e\u5c31\u662f\u4e0b\u8f7d\u5230\u672c\u5730\u54c8\u54c8 import requests  &hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/flighhai.cn\/index.php?rest_route=\/wp\/v2\/posts\/1068"}],"collection":[{"href":"https:\/\/flighhai.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flighhai.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flighhai.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/flighhai.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1068"}],"version-history":[{"count":4,"href":"https:\/\/flighhai.cn\/index.php?rest_route=\/wp\/v2\/posts\/1068\/revisions"}],"predecessor-version":[{"id":1072,"href":"https:\/\/flighhai.cn\/index.php?rest_route=\/wp\/v2\/posts\/1068\/revisions\/1072"}],"wp:attachment":[{"href":"https:\/\/flighhai.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flighhai.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flighhai.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}