Skip to content

Instantly share code, notes, and snippets.

@noahbroyles
Last active October 25, 2021 16:48
Show Gist options
  • Save noahbroyles/ef41f237b3426dbd7ae43877c5475d99 to your computer and use it in GitHub Desktop.
Save noahbroyles/ef41f237b3426dbd7ae43877c5475d99 to your computer and use it in GitHub Desktop.
Get autocomplete search suggestions from YouTube
"""
YOUTUBE SEARCH SUGGESTIONS
Author: Noah Broyles
This program shows how to get search autocomplete suggestions from YouTube with Python. This is the same API that YouTube uses at youtube.com.
If you look at the Network log while searching something on YouTube, you will see that a request is made for EACH keypress in
the search box. This API is super fast, built by Google to stand up to high demand.
Because this is just a reverse engineered web API, YouTube could change their method of getting search suggestions at any time and this could
stop working. I'm betting they will keep this for a while, however.
Last Confirmed Working: 09/24/2021
"""
import json, requests
from urllib import parse
# Search query goes here
q = parse.quote("jar of hear")
# Call the API
r = requests.get(f"https://suggestqueries-clients6.youtube.com/complete/search?client=youtube&hl=en&gl=us&gs_rn=64&gs_ri=youtube&ds=yt&q={q}&xhr=t&xssi=t", headers={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.115 Safari/537.36"})
# Split out junk content and get a JSON object from the response
response = json.loads(r.text.split('\n')[1])
# Create a list to store suggestions
suggestions = []
# Recursive function to get suggestions from the response
def get_suggestions(element):
for suggestion in element:
if isinstance(suggestion, str):
suggestions.append(suggestion)
elif isinstance(suggestion, list):
get_suggestions(suggestion)
# Decode youtube's rather crappy/messy API response
get_suggestions(response)
print(suggestions)
# Output from suggestions:
"""
['jar of hear', 'jar of hearts', 'jar of hearts lyrics', 'jar of hearts karaoke', 'jar of hearts christina perri', 'jar of hearts the voice', 'jar of hearts cover', 'jar of hearts remix', 'jar of hearts glee', 'jar of hearts piano', 'jar of hearts twenty one pilots', 'jar of hearts remix tik tok', 'jar of hearts live', 'jar of hearts nightcore', 'jar of hearts karaoke higher key']
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment