Skip to content Skip to sidebar Skip to footer

How To Check If A Youtube Video Exists Using Python?

I've got a this simple function, that checks if a website exists: def try_site(url): request = requests.get(url) return request.status_code == 200 The issue is, that for y

Solution 1:

Fetch the web page and check if there is a text like "Video unavailable" or alike in the page.


Solution 2:

import requests

pattern = '"playabilityStatus":{"status":"ERROR","reason":"Video unavailable"'


def try_site(url):
    request = requests.get(url)
    return False if pattern in request.text else True


print(try_site("https://youtu.be/dQw4w9WgXcQ"))
print(try_site("https://youtu.be/dQw4w9WgXcR"))

Solution 3:

The easiest you can do (that at least works for youtube):

EDIT: this would be more of checking if the specific url can be directly successfully accessed

def try_site(url):
    request = requests.get(url, allow_redirects=False)
    return request.status_code == 200

disallow redirects and check for status code 200 specifically, there is a slight issue in that status code 302 for example (which is returned in case of invalid url for youtube) means redirecting to some other url that is mentioned in the header, in short tho that means that the specific url you are searching for is not accessible directly, there is no content there, but that doesn't mean that the specific content doesn't exist, it may have been moved to some other place on the server (that may be mentioned in the header) (in case of youtube probably not)


Post a Comment for "How To Check If A Youtube Video Exists Using Python?"