Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytube library - Receiving "pytube.exceptions.RegexMatchError: regex pattern" error when attempting to access video data

When attempting to access data such as the "title" of a Youtube video using the pytube API library, I am receiving the following error

pytube.exceptions.RegexMatchError: regex pattern (yt.akamaized.net/)\s*\|\|\s*.?\sc\s*&&\sd.set([^,]+\s,\s*(?P[a-zA-Z0-9$]+)() had zero matches

Here is the code that I am using which is providing the error above.

import sys  
import pytube

link = input('Enter url: ') # url is: https://www.youtube.com/watch?v=KSbj-cFtFPA
yt = pytube.YouTube(link)
print(yt.title)

sys.exit()

Might anyone know how to resolve this error? I have done some on-line searching, the answers provided were not definitive and did not work when I try.

Note: I tried to uninstall pytube and re-install it but that did not fix the issue.


1 Answers

There is a bug in pytube, so reinstall it using this command.

pip install git+https://github.com/nficano/pytube.git

#####################################################

import sys
import pytube

link = input('Enter url: ') # url is: https://www.youtube.com/watch?v=KSbjcFtFPA
yt = pytube.YouTube(link)
print(yt.title)

**OUTPUT** Sanford and Son S02E02

sys.exit()

Here is some additional information on this bug: https://github.com/nficano/pytube/issues/333

This is a follow-up response to the question. I added some error handling based on the documentation to tell you what is occurring.

- Video 1 in the list will throw an extraction error. because the video has been removed from YouTube

- Video 2 in the list will throw an unavailable error. because the video does not exist on YouTube

- Video 3 in the list will display the correct title info, because the video exists on YouTube.

video_lists = ['https://www.youtube.com/watch?v=KSbjcFtFPA',
           'https://www.youtube.com/watch?v=MEEJOZkmIxvU',
           'https://www.youtube.com/watch?v=MEEJOZkmIxU'
           ]

for video in video_lists:

  try:
    yt = pytube.YouTube(video)
    print (yt.title)

  #################################################################
  # This one should catch - pytube.exceptions.RegexMatchError:
  # regex pattern ((?:v=|\/)([0-9A-Za-z_-]{11}).*) had zero matches
  #################################################################
  except pytube.exceptions.RegexMatchError:
    print('The Regex pattern did not return any matches for the video: {}'.format(video))

  except pytube.exceptions.ExtractError:
    print ('An extraction error occurred for the video: {}'.format(video))

  except pytube.exceptions.VideoUnavailable:
    print('The following video is unavailable: {}'.format(video))

**OUTPUTS**
An extraction occurred for the video: https://www.youtube.com/watch?v=KSbjcFtFPA

The following video is unavailable: https://www.youtube.com/watch?v=MEEJOZkmIxvU

Love of my life & Bohemian Rhapsody - 1080 HD

SPECIAL NOTE

The error in question has been linked to the file cipher.py for pytube. Please check this file to ensure that the section of code below matches yours:

def get_initial_function_name(js):
"""Extract the name of the function responsible for computing the signature.

:param str js:
    The contents of the base.js asset file.

"""
# c&&d.set("signature", EE(c));
pattern = r'\bc\s*&&\s*d\.set\([^,]+\s*,\s*\([^)]*\)\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\('
logger.debug('finding initial function name')
return regex_search(pattern, js, group=1)
like image 63
Life is complex Avatar answered Sep 08 '25 05:09

Life is complex