如何提取字符串的一部分,从一个词到另一个词?
How to extract part of a string, from one word to the other?
当我 运行 下面的代码得到一个很大的 JSON 响应时,我如何才能只提取视频的 URL?
from youtubesearchpython import VideosSearch
title = 'Video Name'
videosSearch = VideosSearch(title, limit = 0)
print(videosSearch.result())
{
"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 的新手,如有任何帮助,我们将不胜感激。
videosSearch.result()
只是一个字典,所以你可以像这样得到所有 url:
print([result["link"] for result in videosSearch.result()["result"]])
videosSearch.result()["result"][0]["link"]
应该就是你要找的。
from youtubesearchpython import VideosSearch
title = 'Video Name'
videosSearch = VideosSearch(title, limit = 0)
response = videosSearch.result()
print([data["link"] for data in response["result"])
P.S。在写答案的时候,已经有人回答了:)
当我 运行 下面的代码得到一个很大的 JSON 响应时,我如何才能只提取视频的 URL?
from youtubesearchpython import VideosSearch
title = 'Video Name'
videosSearch = VideosSearch(title, limit = 0)
print(videosSearch.result())
{
"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 的新手,如有任何帮助,我们将不胜感激。
videosSearch.result()
只是一个字典,所以你可以像这样得到所有 url:
print([result["link"] for result in videosSearch.result()["result"]])
videosSearch.result()["result"][0]["link"]
应该就是你要找的。
from youtubesearchpython import VideosSearch
title = 'Video Name'
videosSearch = VideosSearch(title, limit = 0)
response = videosSearch.result()
print([data["link"] for data in response["result"])
P.S。在写答案的时候,已经有人回答了:)