Unable to scrape Youtube video duration with XPathOnURL

=XPathOnUrl(https://www.youtube.com/watch?v=kYparHxVnpk,"//*[@id='movie_player']/div[25]/div[2]/div[1]/div/span[3]")

I'm trying to use the formula above to scrape the video duration time from a given Youtube page. But for some reason, I cannot seem to get it to work. I can drill down to the time in the code when using the Inspect tool in Chrome, and then get an Xpath for it, but it keeps returning blank results in Excel.

I'm not sure where in the code you found the duration but I tried this one and it returned blank because it's behind javascript:

You can get around this with the Phantom JS Cloud Connector (required signup to get access key). This request returns the duration:

=Connector("PhantomJsCloud.XPath";"https://www.youtube.com/watch?v=kYparHxVnpk";"//span[@class='ytp-time-duration']";"";TRUE;"us")

The easiest is probably to use the YouTube API. Will update it with an endpoint that returns duration.

1 Like

Also, if you use the URL in the formula instead of referencing a cell which contains the URL, you need quotation marks.

1 Like

Duration in youtube is stored in meta too.
So you can use this xpath:
//meta[@itemprop='duration']/@content
You'll get duration in format ISO 8601 like PT54M51S.

2 Likes

Thanks for the reply. I tried that, but I am getting a blank result. My formula is below (where B2 is referencing the cell which contains the URL to the Youtube video)

=XPathOnUrl(B2,"//meta[@itemprop='duration']/@content")