Key Error of 'Assets' when attempting to call 'YouTube()'
See original GitHub issueWhen doing the following command:
from pytube import YouTube
yt = YouTube('http://youtube.com/watch?v=9bZkp7q19f0')
I’m seeing the key error ‘assets’:
KeyError Traceback (most recent call last) <ipython-input-2-4ab0b1497744> in <module> ----> 1 yt = YouTube(‘http://youtube.com/watch?v=9bZkp7q19f0’)
C:\Program Files\Anaconda3\lib\site-packages\pytube_main_.py in init(self, url, defer_prefetch_init, on_progress_callback, on_complete_callback, proxies) 89 90 if not defer_prefetch_init: —> 91 self.prefetch() 92 self.descramble() 93
C:\Program Files\Anaconda3\lib\site-packages\pytube_main_.py in prefetch(self) 181 self.vid_info_raw = request.get(self.vid_info_url) 182 if not self.age_restricted: –> 183 self.js_url = extract.js_url(self.watch_html) 184 self.js = request.get(self.js_url) 185
C:\Program Files\Anaconda3\lib\site-packages\pytube\extract.py in js_url(html) 141 The html contents of the watch page. 142 “”" –> 143 base_js = get_ytplayer_config(html)[“assets”][“js”] 144 return “https://youtube.com” + base_js 145
KeyError: ‘assets’
Issue Analytics
- State:
- Created 3 years ago
- Comments:14
Top GitHub Comments
Hi, Since yesterday I have the same, on every version of pytube (pytube, pytube3, pytubex). I thought youtube banned my ip address because I downloaded almost 300GB yesterday and it suddenly stopped working. It looks like youtube changed something yesterday which made pytube not working at all.
I found a problem. I don’t know regex, so my solution is primitive but works fully. There is a js_url() function in the extract.py file. Delete it completely (or rename it). Create a new js_url() function, it should look like this:
def js_url(html: str) -> str: start = html.find('src="/s/player/') +5 stop = html[start:].find('base.js') + 7 js_url = html[start:start + stop] return "https://youtube.com" + js_url
Doesn’t work with age-restricted videos, but now I know why, I’ll post a revised code tomorrow. Today I downloaded over 500 videos without any errors.
@vincehartman38 @AssesBesses this problem was fixed in the most recent update to the repository, so you will need to reinstall pytube in order to fix it.
To install from this repository, I recommend you first uninstall pytube with
pip uninstall pytube
, then re-install it withpython -m pip install git+https://github.com/nficano/pytube
.