从YouTube链接中检索视频ID

时间:2018-03-13 07:29:14

标签: python python-3.x

说我想要此链接中的YouTube视频ID(VgC4b9K-gYU):

https://www.youtube.com/watch?v=VgC4b9K-gYU

我如何使用Python获取它?

2 个答案:

答案 0 :(得分:3)

您需要使用'urlparse'库。

Python2

>>> import urlparse
>>> url = "https://www.youtube.com/watch?v=VgC4b9K-gYU"
>>> url_parsed = urlparse.urlsplit(url)
>>> qsl = urlparse.parse_qs(url_parsed.query)
>>> qsl
{'v': ['VgC4b9K-gYU']}

Python3

from urllib import parse
url = "https://www.youtube.com/watch?v=VgC4b9K-gYU"
url_parsed = parse.urlparse(url)
qsl = parse.parse_qs(url_parsed.query)

答案 1 :(得分:0)

可能有更多方法可以做到,但我希望urlparser library能够做到这一点。

试试这个:

[
    { "OBECT_TAG":
      {
        "PROPERTY1" : "value1",
        "PROPERTY2" : "value2"
      }
    }
]

<强>输出:

import urlparse3

url = 'https://www.youtube.com/watch?v=VgC4b9K-gYU'
parsed_url = urlparse3.parse_url(url)
print parsed_url.query # Ordered-Dictionary

for k, v in parsed_url.query.items():
    print 'The Key you want is : ' + v

希望这会对你有所帮助。谢谢! :)