Search code examples
pythonpython-3.xpython-3.8pytube

Pytube only works periodically (KeyError: 'assets')


Five out of ten times Pytube will send me this error when attempting to run my small testing script.

Here's the script:

import pytube
import urllib.request


from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=3NCyD3XoJgM')

print('Youtube video title is: ' + yt.title + '! Downloading now!')

Here's what I get:

Traceback (most recent call last):
  File "youtube.py", line 6, in <module>
    yt = YouTube('https://www.youtube.com/watch?v=3NCyD3XoJgM')
  File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\__main__.py", line 91, in __init__
    self.prefetch()
  File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\__main__.py", line 183, in prefetch
    self.js_url = extract.js_url(self.watch_html)
  File "C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pytube\extract.py", line 143, in js_url
    base_js = get_ytplayer_config(html)["assets"]["js"]
KeyError: 'assets'

I am very confused. I attempted to reinstall Python plus pytube but I can't seem to remedy this issue. It's increasingly perplexing that the script works half of the time, but not the other half.


Solution

  • For now fixed 100% with this:

    https://github.com/nficano/pytube/pull/767#issuecomment-716184994

    With anyone else getting this error or issue, run this command in a terminal or cmd: python -m pip install git+https://github.com/nficano/pytube

    An update to pytubeX that hasn't been released with the pip installation yet. The GitHub link is the current dev explaining the situation.