Skip to content

Instantly share code, notes, and snippets.

Last active July 4, 2022 06:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Web Scraping YouTube Popular Videos Results Data with Python and SerpApi
# video tutorial:
from serpapi import GoogleSearch
from urllib.parse import (parse_qsl, urlsplit)
import json
params = {
"api_key": "your api key", # your serpapi api key
"engine": "youtube", # serpapi parsing engine
"search_query": "blender foundation", # search query
"gl": "US", # country of the search
"hl": "en", # language of the search
# "sp": "EgJ4AQ%253D%253D" # youtube filter
search = GoogleSearch(params)
video_results = []
videos_amount = []
while True:
results = search.get_dict()
if "playlist_results" in results:
pass # extract only playlist results or whatever you want from the output
for result in results.get("video_results", []):
"title": result.get("title"),
"link": result.get("link"),
"published_date": result.get("published_date"),
"views": result.get("views"),
"length": result.get("length"),
"channel": result.get("channel")
if "next" in results.get("serpapi_pagination", {}):
search.params_dict.update(dict(parse_qsl(urlsplit(results.get("serpapi_pagination", {}).get("next")).query)))
Copy link

dimitryzub commented Jul 4, 2022

1. Apply desired YouTube filter on YouTube itself:

2. Copy sp= query parameter value from URL:

CAM%253D = filter by views.

3. Pass this value to SerpApi:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment