【问题标题】:How to extract part of a string, from one word to the other?如何提取字符串的一部分,从一个单词到另一个单词?
【发布时间】:2021-05-07 02:34:32
【问题描述】:

当我运行下面的代码时,我得到一个很大的 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 新手,如有任何帮助,将不胜感激。

【问题讨论】:

    标签: python string replace substring


    【解决方案1】:

    videosSearch.result()["result"][0]["link"] 应该是你要找的。​​p>

    【讨论】:

    • 感谢您的帮助!
    • @Code_Python 没问题!如果您将我的答案标记为已接受,我将不胜感激,因为它为我们双方提供了代表。
    【解决方案2】:

    videosSearch.result() 只是一个字典,所以你可以像这样获取所有 url:

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

    【讨论】:

    • 非常感谢!
    【解决方案3】:
    from youtubesearchpython import VideosSearch
    
    title = 'Video Name'
    videosSearch = VideosSearch(title, limit = 0)
    
    response = videosSearch.result()
    
    print([data["link"] for data in response["result"])
    
    

    附:在写答案的时候,有人已经回答了:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 2016-09-10
      • 2020-09-01
      • 2015-05-08
      • 1970-01-01
      • 2020-03-22
      相关资源
      最近更新 更多