import requestsfrom urllib.parse import urlencodeurl = 'https://www.youtube.com/watch?v=Xe8fIjxicoo'query_string = urlencode({'url': url, 'format': 'json'})oembed_url = f"http://www.youtube.com/oembed?{query_string}"r = requests.get(oembed_url)if r.status_code == 200: result = r.json() html = result['html'].strip()
{
"author_url":"https:\/\/www.youtube.com\/user\/TEDtalksDirector",
"version":"1.0",
"title":"How we must respond to the COVID-19 pandemic | Bill Gates",
"thumbnail_height":360,
"type":"video",
"width":480,
"provider_name":"YouTube",
"thumbnail_width":480,
"html":"\u003ciframe width=\"480\" height=\"270\" src=\"https:\/\/www.youtube.com\/embed\/Xe8fIjxicoo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen\u003e\u003c\/iframe\u003e",
"provider_url":"https:\/\/www.youtube.com\/",
"author_name":"TED",
"height":270,
"thumbnail_url":"https:\/\/i.ytimg.com\/vi\/Xe8fIjxicoo\/hqdefault.jpg"
}
NOTE: Just embed the html
and the player will load without additional javascript required.
References: