0

I want to scrap Youtube Video (not with pytube or any other modules) with using python (requests, selenium etc) but do not want to open window like selenium does..

i can use requests but problem is, youtube use dynamically javascript to create elements which cant be scraped by requests module, but using selenium to encounter this problem is opening windows and i want my code to fully functional in terminal (so that i can use it in my django website).. is there any way to do it?

import requests, bs4

print('Getting title of YT video..')

url = 'https://www.youtube.com/watch?v=Xw9XFtzIIV8&ab_channel=MemeJuice'
res = requests.get(url)

soup = bs4.BeautifulSoup(res.content, 'html.parser')
title = soup.find('h1', {'class':'title'})
print('Title of Video is %s' %title)
Verma
  • 173
  • 9
  • Does this answer your question? [Can Selenium WebDriver open browser windows silently in the background?](https://stackoverflow.com/questions/16180428/can-selenium-webdriver-open-browser-windows-silently-in-the-background) – nfn Feb 14 '22 at 06:12

1 Answers1

0

It's much better to use the YouTube API.

Unfortunately, It is not possible to get the video title using the video ID with API v3 without the API key if you use the API directly. The YouTube Data API v2 is deprecated (see: YouTube Data API v2 Deprecation: Frequently Asked Questions) and currently the YouTube API doesn't support oEmbed with JSONP as it should (see Issue 4329: oEmbed callback for JSONP).

But fortunately there is the Noembed service that lets you get the titles (and other data) of YouTube videos with JSONP and without the API key.

Use can go to,

https://noembed.com/embed?url=https://www.youtube.com/watch?v=dQw4w9WgXcQ

And change the ID to the id you want to get basic data in json like,

```{"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ","version":"1.0","thumbnail_height":360,"provider_name":"YouTube","width":200,"thumbnail_width":480,"title":"Rick Astley - Never Gonna Give You Up (Official Music Video)","thumbnail_url":"https://i.ytimg.com/vi/dQw4w9WgXcQ/hqdefault.jpg","author_name":"Rick Astley","author_url":"https://www.youtube.com/c/RickastleyCoUkOfficial","html":"<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/dQw4w9WgXcQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>","height":113,"provider_url":"https://www.youtube.com/","type":"video"}```

Backend code using python

import json
from requests import get
data = dict(json.loads(get("https://noembed.com/embed?url=https://www.youtube.com/watch?v="+id).text))
title = data['title']

You can also use jQuery to get the data from the frontend,

var id = 'dQw4w9WgXcQ';
var url = 'https://www.youtube.com/watch?v=' + id;

$.getJSON('https://noembed.com/embed',
    {format: 'json', url: url}, function (data) {
    alert(data.title);
});