
#!/usr/bin/env python3įrom urllib.request import urlopen, FancyURLopenerįrom urllib.parse import urlparse, parse_qs, unquoteĬlass UndercoverURLopener(FancyURLopener): Finally, it masks its user-agent as Chrome for Mac (which is what I currently use). It also takes any numbers of YouTube URLs and downloads them all. Incidentally, it deals with Unicode video titles by removing non-ASCII characters from the filename to which the video is saved. I've removed some of the try/except which didn't do much but exit, for clarity. Here's a cleaner version, which uses urllib to parse URLs and query strings, and which successfully downloads a video. You might have tripped on any of those specificities. I tested it and it might depend even on which related videos show on the page. The code on the original question relies on several assumptions about the content of YouTube pages and URLs (expressed in constructs such as "url.split('something=')") which may not always be true. Raise HTTPError(req.full_url, code, msg, hdrs, fp) Return (new, timeout=req.timeout)įile "C:\Python31\lib\urllib\request.py", line 391, in errorįile "C:\Python31\lib\urllib\request.py", line 473, in http_error_default 'http', request, response, code, msg, hdrs)įile "C:\Python31\lib\urllib\request.py", line 385, in errorįile "C:\Python31\lib\urllib\request.py", line 325, in _call_chainįile "C:\Python31\lib\urllib\request.py", line 560, in http_error_302 There’s an error message (thanks Wooble): Traceback (most recent call last):įile "C:/Python31/MyLib/DrawingBoard/youtube_download-.py", line 52, in įile "C:\Python31\lib\urllib\request.py", line 119, in urlopenįile "C:\Python31\lib\urllib\request.py", line 353, in openįile "C:\Python31\lib\urllib\request.py", line 465, in http_response The video is saved to the current working directory(cwd).\n") Print(" Downloading " + '"' + video_title + '".')ĭownload = (download_url).read() Video_title = video_title.replace('&', '&')

Video_title = video_title.replace(''', '"') Token_value = url.split('video_id=' + video_id + '&token=').split('&thumbnail_url')ĭownload_url = "" + video_id + "&t=" + token_value + "&fmt=18"

Video_id = video_url.split('elif(video_url.endswith('&feature=channel_page')): Video_id = video_url.split('elif(video_url.endswith('&feature=fvst')):

Video_id = video_url.split('elif(video_url.endswith('&feature=dir')): If(video_url.endswith('&feature=related')): download = (download_url).read() # YouTube video download script I’m having trouble retrieving the YouTube video automatically.
