0

These two inputs should have the same title:

https://www.youtube.com/watch?v=LAUa5RDUvO4

https://youtu.be/LAUa5RDUvO4

But I can't get both to work in one program, seemingly because the ID (in here, LAUa5RDUvO4) cannot be found for the shorturl.

How can I print the title using Youtube API?

P.Y.C.
  • 151
  • 1
  • 12
  • Does this answer your question? [Get title from YouTube videos](https://stackoverflow.com/questions/1216029/get-title-from-youtube-videos) – mozilla-firefox Jan 07 '20 at 11:14
  • Welcome to stackoverflow. This isn't a free code writing service. Neither is it a replacement for tutorials or web searches. Please read [ask]. Then [edit] your question and add the code you've tried so far. What happens when you run it? What did you expect to happen instead? Any errors? – Robert Jan 07 '20 at 18:05
  • edited, sorry for that. – P.Y.C. Jan 08 '20 at 06:19

2 Answers2

6

You can use this code to get the video title. All you need is just video id. This question already has an answer Get title from YouTube videos . Hope question gets duplicated. The below answer is by porto which I find to be useful.

import urllib.request
import json
import urllib

#change to yours VideoID or change url inparams
VideoID = "LAUa5RDUvO4" 

params = {"format": "json", "url": "https://www.youtube.com/watch?v=%s" % VideoID}
url = "https://www.youtube.com/oembed"
query_string = urllib.parse.urlencode(params)
url = url + "?" + query_string

with urllib.request.urlopen(url) as response:
    response_text = response.read()
    data = json.loads(response_text.decode())
    print(data['title'])
mozilla-firefox
  • 864
  • 1
  • 15
  • 23
1

You can use lxml parser and xpath expressions to extract the title of a youtube video

import lxml
from lxml import etree
youtube = etree.HTML(urllib.urlopen("http://www.youtube.com/watch?v=KQEOBZLx-Z8").read()) //enter your youtube url here
video_title = youtube.xpath("//span[@id='eow-title']/@title") //get xpath using firepath firefox addon
print ''.join(video_title)
Sami Belkacem
  • 336
  • 3
  • 12