Can somebody help me with this code? I'm trying to make a python script that will play videos and I found this file that download's Youtube videos. I am not entirely sure what is going on and I can't figure out this error.
Error:
AttributeError: 'NoneType' object has no attribute 'group' Traceback:
Traceback (most recent call last): File "youtube.py", line 67, in <module> videoUrl = getVideoUrl(content) File "youtube.py", line 11, in getVideoUrl grps = fmtre.group(0).split('&') Code snippet:
(lines 66-71)
content = resp.read() videoUrl = getVideoUrl(content) if videoUrl is not None: print('Video URL cannot be found') exit(1) (lines 9-17)
def getVideoUrl(content): fmtre = re.search('(?<=fmt_url_map=).*', content) grps = fmtre.group(0).split('&') vurls = urllib2.unquote(grps[0]) videoUrl = None for vurl in vurls.split('|'): if vurl.find('itag=5') > 0: return vurl return None
The error is in your line 11, your re.search is returning no results, ie None, and then you're trying to call fmtre.group but fmtre is None, hence the AttributeError.
You could try:
def getVideoUrl(content): fmtre = re.search('(?<=fmt_url_map=).*', content) if fmtre is None: return None grps = fmtre.group(0).split('&') vurls = urllib2.unquote(grps[0]) videoUrl = None for vurl in vurls.split('|'): if vurl.find('itag=5') > 0: return vurl return None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With