如何使用Python 3.11从JSON中提取视频文件?

8zzbczxx  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(183)

我需要从这个JSON对象中提取一个视频文件。例如,我有两个Episode,我想拉 * Episode 1 *,并下载它。我该怎么做呢?
{"list ":{"1":{" episode ":1、"名称":null," uuid ":"95cbd36e-789e-11ec-ae92 - 0242ac120002","created_timestamp":1571078646,“预览”:null,"跳过":{"opening ":[],"结束":[]}," hls ":{" fhd":"/videos/media/ts/8499/1/1080/9186f5d125c799f16965a9b3a38a2c42.m3u8 "," hd ":"/videos/media/ts/8499/1/720/5a310e07f181fb31a7d9b69f7a62b4c8.m3u8","sd":"/videos/media/ts/8499/1/480/0fc60668a8713fbe55bb0cec4b7dc0d4.m3u8 "}},"2":{"episode ":2、"名称":null," uuid ":" 95cbd3e3 - 789e-11ec-ae92 - 0242ac120002","created_timestamp":1571722736,“预览”:null,"跳过":{" opening ":[],"结束":[]}," hls":{" fhd":"/videos/media/ts/8499/2/1080/314fd5a58d5d08be401be695e5a37e03.m3u8"," hd":"/videos/media/ts/8499/2/720/84ce5fdff2cb0fca90db72f900e94359.m3u8"," sd":"/videos/media/ts/8499/2/480/4f4e483ac65fb1c24ea90dd1d053c128.m3u8 "}}
我找不到任何例子,或者知道如何做到这一点的人。
我尝试做某事:

import requests

user_input = input("Enter anime title: ")

url = f'https://api.anilibria.tv/v3/title/search?search={user_input}'
res = requests.get(url)
res.raise_for_status()
data = res.json()

titles_and_links = [] 

for entry in data['list']:
    try:
        title = entry['names']['ru']  
        video_link = entry['episode'][0]['fhd']  

        data_item = {
            'title': title,
            'video_link': video_link
        }
        titles_and_links.append(data_item)
    except KeyError as e:
        print(f"Skipping entry due to missing data: {e}")

for item in titles_and_links:
    print("Title:", item['title'])
    print("Video Link:", item['video_link'])
    print()
6ojccjat

6ojccjat1#

IIUC,你想从名为list的字典中获取情节1(顺便说一句,非常不幸的命名):

import requests

user_input = input("Enter anime title: ")

url = f"https://api.anilibria.tv/v3/title/search?search={user_input}"
res = requests.get(url)
res.raise_for_status()
data = res.json()

titles_and_links = []

for entry in data["list"]:
    title = entry["names"]["ru"]
    e = entry["player"]["list"]["1"]  # <-- get first episode (`1`) from the `list`
    video_link = e["hls"]["fhd"]
    titles_and_links.append({"title": title, "video_link": video_link})

for item in titles_and_links:
    print("Title:", item["title"])
    print("Video Link:", item["video_link"])
    print()

打印(例如):

Enter anime title: chan
Title: Девочка, которая видит это
Video Link: /videos/media/ts/9066/1/1080/84d1352caf37e1e74a85fe4b8ad0c3df.m3u8

Title: Дропкик злого духа 2
Video Link: /videos/media/ts/8599/1/1080/595f97d57725538be78cf227f5c31304.m3u8

Title: Дропкик злого духа
Video Link: None

Title: Дэми-тян хочет рассказать
Video Link: None

Title: Мой братик больше не братик!
Video Link: /videos/media/ts/9354/1/1080/e41695fa198835ea5fe8778d4200b53b.m3u8

相关问题