How to scrape Youtube Video URLS based on Keywords?

podninja

Junior Member
Joined
Mar 29, 2012
Messages
172
Reaction score
175
Hi All,

Please recommend me a tool or method to scrape youtube videos based on keywords. Please don't say do it manually :P

Thanks
 
if you have scrapebox you could do it like this :

site:youtube.com keyword1
site:youtube.com keyword2
 
this sample with python without scrolling emulation..
Python:
import os
import requests
import urllib.parse
import json


class YtSearch:
    def __init__(self, search_terms: str, max_results=None):
        self.search_terms = search_terms
        self.max_results = max_results
        self.videos = self.search()

    def search(self):
        encoded_search = urllib.parse.quote(self.search_terms)
        BASE_URL = "https://youtube.com"
        url = f"{BASE_URL}/results?search_query={encoded_search}"
        response = requests.get(url).text
        while 'window["ytInitialData"]' not in response:
            response = requests.get(url).text
        results = self.parse_html(response)
        if self.max_results is not None and len(results) > self.max_results:
            return results[: self.max_results]
        return results

    def parse_html(self, response):
        results = []
        start = (
            response.index('window["ytInitialData"]')
            + len('window["ytInitialData"]')
            + 3
        )
        end = response.index("};", start) + 1
        json_str = response[start:end]
        data = json.loads(json_str)

        videos = data["contents"]["twoColumnSearchResultsRenderer"]["primaryContents"][
            "sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"]

        for video in videos:
            res = {}
            if "videoRenderer" in video.keys():
                video_data = video.get("videoRenderer", {})
                res["id"] = video_data.get("videoId", None)
                res["title"] = video_data.get("title", {}).get("runs", [[{}]])[0].get("text", None)
                results.append(res)
        return results

    def to_dict(self):
        return self.videos


def main():
    kwd = str(input('Enter keyword: '))
    results = YtSearch(kwd, max_results=20).to_dict()
    for x in results:
        uri = 'https://www.youtube.com/watch?v='+str(x['id'])
        title = str(x['title'])
        print(title, uri)


if __name__ == '__main__':
    main()
you can use youtube-dl also...
 
this sample with python without scrolling emulation..
Python:
import os
import requests
import urllib.parse
import json


class YtSearch:
    def __init__(self, search_terms: str, max_results=None):
        self.search_terms = search_terms
        self.max_results = max_results
        self.videos = self.search()

    def search(self):
        encoded_search = urllib.parse.quote(self.search_terms)
        BASE_URL = "https://youtube.com"
        url = f"{BASE_URL}/results?search_query={encoded_search}"
        response = requests.get(url).text
        while 'window["ytInitialData"]' not in response:
            response = requests.get(url).text
        results = self.parse_html(response)
        if self.max_results is not None and len(results) > self.max_results:
            return results[: self.max_results]
        return results

    def parse_html(self, response):
        results = []
        start = (
            response.index('window["ytInitialData"]')
            + len('window["ytInitialData"]')
            + 3
        )
        end = response.index("};", start) + 1
        json_str = response[start:end]
        data = json.loads(json_str)

        videos = data["contents"]["twoColumnSearchResultsRenderer"]["primaryContents"][
            "sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"]

        for video in videos:
            res = {}
            if "videoRenderer" in video.keys():
                video_data = video.get("videoRenderer", {})
                res["id"] = video_data.get("videoId", None)
                res["title"] = video_data.get("title", {}).get("runs", [[{}]])[0].get("text", None)
                results.append(res)
        return results

    def to_dict(self):
        return self.videos


def main():
    kwd = str(input('Enter keyword: '))
    results = YtSearch(kwd, max_results=20).to_dict()
    for x in results:
        uri = 'https://www.youtube.com/watch?v='+str(x['id'])
        title = str(x['title'])
        print(title, uri)


if __name__ == '__main__':
    main()
you can use youtube-dl also...

I don't know Coding... I will check few tutorials on how to execute Phyton code.

Actually, I used this code on google sheets and it's working well but I am unable to add Nextpagetoken to this code and I was stuck there... Else it's a free method.


Code:
function YouTubeScraper() {
  var sh1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var keyword = sh1.getRange("B1").getValue();
 
  var results = YouTube.Search.list('id,snippet', {q:keyword, maxResults:50});
//Video ID    Published Date    Channel ID    "Video Title
//"    Description    Thumbnail URL    Channel Title
  var items = results.items.map(function(e){
    return [e.id.videoId,
            e.snippet.publishedAt,
            e.snippet.channelId,
            e.snippet.title,
            e.snippet.description,
            e.snippet.thumbnails["default"].url,
            e.snippet.channelTitle]
  })
   sh1.getRange(4, 1, items.length, items[0].length).setValues(items)       
        
}
 
Something to mention better to use bing.com instead of google, bing has site: operator but they are not nazi like google regarding scraping they will not ban your proxies too fast like google.
Yeah... I understand... Google is real Nazi. Thanks for the useful info.
 
I'd have to agree.. what do you want from the scrape. That would help to isolate a tool for this. For example.. would you be looking for expired domains from links in the youtube videos descriptions ? Your question is super broad.
 
I'd have to agree.. what do you want from the scrape. That would help to isolate a tool for this. For example.. would you be looking for expired domains from links in the youtube videos descriptions ? Your question is super broad.
:)
That’s what I am doing everyday
Looks for expired domain , but it’s pretty hard to find good ones
 
Back
Top