如何从一个词到另一个词提取字符串的一部分?

2024-05-20 23:48:01 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行下面的代码时,我会得到一个大的JSON响应,我如何才能只提取视频的URL

from youtubesearchpython import VideosSearch

title = 'Video Name'
videosSearch = VideosSearch(title, limit = 0)
print(videosSearch.result())

Resul:

{
   "result":[
      {
         "type":"video",
         "id":"JJTqk8P9VO0",
         "title":"Kinemaster | how to create name intro || video editing tutorial",
         "publishedTime":"2 years ago",
         "duration":"9:02",
         "viewCount":{
            "text":"3,133,469 views",
            "short":"3.1M views"
         },
         "thumbnails":[
            {
               "url":"https://i.ytimg.com/vi/JJTqk8P9VO0/hq720.jpg?sqp=-oaymwEjCOgCEMoBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLAbhbrfMi-lkezgeIPVf--18BmW4A",
               "width":360,
               "height":202
            },
            {
               "url":"https://i.ytimg.com/vi/JJTqk8P9VO0/hq720.jpg?sqp=-oaymwEXCNAFEJQDSFryq4qpAwkIARUAAIhCGAE=&rs=AOn4CLCwJanhvro5scsjaWV8fRjfv81rvg",
               "width":720,
               "height":404
            }
         ],
             "descriptionSnippet":[
            {
               "text":"Kinemaster "
            },
            {
               "text":"video",
               "bold":true
            },
            {
               "text":" editing tutorial how to create "
            },
            {
               "text":"name",
               "bold":true
            },
            {
               "text":" intro android\\xa0..."
            }
         ],
         "channel":{
            "name":"Samir creation,s",
            "id":"UCE_4iIKu3jzS0ylJcZyhPJQ",
            "thumbnails":[
               {
                  "url":"https://yt3.ggpht.com/ytc/AAUvwniOMvDEaCNKl_vAuSjEyixNPCOdu20DR-EUSSQHKw=s68-c-k-c0x00ffffff-no-rj",
                  "width":68,
                  "height":68
               }
            ],
            "link":"https://www.youtube.com/channel/UCE_4iIKu3jzS0ylJcZyhPJQ"
         },
         "accessibility":{
            "title":"Kinemaster | how to create name intro || video editing tutorial by Samir creation,s 2 years ago 9 minutes, 2 seconds 3,133,469 views",
            "duration":"9 minutes, 2 seconds"
         },
         "link":"https://www.youtube.com/watch?v=JJTqk8P9VO0",
         "shelfTitle":"None"
      }
   ]
}

我是Python新手,需要任何帮助。


Tags: totextnamehttpscomtitlevideocreate
3条回答
from youtubesearchpython import VideosSearch

title = 'Video Name'
videosSearch = VideosSearch(title, limit = 0)

response = videosSearch.result()

print([data["link"] for data in response["result"])

另外,在写答案时,有人已经回答:)

videosSearch.result()["result"][0]["link"]应该是您正在寻找的

videosSearch.result()只是一个dict,因此您可以获得如下所有url:

print([result["link"] for result in videosSearch.result()["result"]])

相关问题 更多 >