The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Scrape trending YouTube videos

To scrape trending YouTube videos, you can use several approaches depending on your technical skills and purpose (e.g., real-time display, regular updates, data analysis). Here’s a Python-based method using the YouTube Data API v3 (official and reliable) and another using web scraping with Selenium (less stable due to frequent layout changes on YouTube’s website).


✅ Method 1: Using YouTube Data API v3

Requirements:

  • Google Developer Account

  • YouTube Data API Key

Steps:

  1. Go to Google Cloud Console, create a project.

  2. Enable “YouTube Data API v3”.

  3. Generate an API key.

Python Script:

python
import requests API_KEY = 'YOUR_API_KEY' REGION_CODE = 'US' # Use 'IN' for India, 'GB' for UK, etc. MAX_RESULTS = 10 url = f"https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&chart=mostPopular&regionCode={REGION_CODE}&maxResults={MAX_RESULTS}&key={API_KEY}" response = requests.get(url) data = response.json() for video in data['items']: title = video['snippet']['title'] channel = video['snippet']['channelTitle'] views = video['statistics']['viewCount'] link = f"https://www.youtube.com/watch?v={video['id']}" print(f"Title: {title}nChannel: {channel}nViews: {views}nLink: {link}n")

⚠️ Method 2: Web Scraping with Selenium (Not API-based)

Requirements:

  • Python

  • Selenium

  • Chrome WebDriver

Script Example:

python
from selenium import webdriver from selenium.webdriver.chrome.service import Service from bs4 import BeautifulSoup import time options = webdriver.ChromeOptions() options.add_argument("--headless") driver = webdriver.Chrome(service=Service('path_to_chromedriver'), options=options) driver.get("https://www.youtube.com/feed/trending") time.sleep(5) soup = BeautifulSoup(driver.page_source, 'html.parser') titles = soup.select("h3 a") for title in titles[:10]: print(f"Title: {title.text.strip()}nLink: https://www.youtube.com{title['href']}n") driver.quit()

Note: YouTube often changes its layout; web scraping can break without notice.


Would you like help automating this for regular updates or exporting the results to CSV or JSON?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About