如何更改我们从 youtube-search-python 包中获得的结果?

How to alter the results we get from the youtube-search-python package?

我正在使用 youtube-search-python 1.3.1 模块来搜索有关特定 youtube 视频的详细信息。

这是它的 PyPi 页面的 link:https://pypi.org/project/youtube-search-python/

我的代码:

from youtubesearchpython import SearchVideos
search = SearchVideos("Video", offset = 1, mode = "json", max_results = 1)
print(search.result())

但是,这样做时,它会打印视频的所有详细信息(属性),如下所示:

{
    "search_result": [
        {
            "index": 0,
            "id": "-_4GW6BNIqs",
            "link": "https://www.youtube.com/watch?v=-_4GW6BNIqs",
            "title": "Insideeus - Ecstasy (Official Video)",
            "channel": "InsideeusVEVO",
            "duration": "3:27",
            "views": 299442272,
            "thumbnails": [
                "https://img.youtube.com/vi/-_4GW6BNIqs/default.jpg",
                "https://img.youtube.com/vi/-_4GW6BNIqs/hqdefault.jpg",
                "https://img.youtube.com/vi/-_4GW6BNIqs/mqdefault.jpg",
                "https://img.youtube.com/vi/-_4GW6BNIqs/sddefault.jpg",
                "https://img.youtube.com/vi/-_4GW6BNIqs/maxresdefault.jpg"
            ],
            "channelId": "UCRNE_wppvoMBdj7EAtUnEog"
        }
    ]
}

我只想打印此结果的“link”部分。我试了很多次都无法得到这个。

有谁知道如何修改代码以便只打印“link”?

这是一个字典,您可以通过

等方式访问值
r = search.result()
for d in r['search_result']:
    print(d['link'])

没关系,我找到了答案。

import json
from youtubesearchpython import SearchVideos

search = SearchVideos("Video", offset = 1, mode = "json", max_results = 1)
r = search.result()
res = json.loads(r)
res1 = res['search_result']
res2 = res1[0]
res_fin = res2['link']
print(res_fin)

如果您想要更高级的功能,可以使用searchtube。它还为您提供过滤器选项。

pip install searchtube

可在此处找到文档:https://github.com/shekharchander/searchtube