Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'NoneType' object has no attribute 'group'

Tags:

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('&amp;') 

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('&amp;')     vurls = urllib2.unquote(grps[0])     videoUrl = None     for vurl in vurls.split('|'):         if vurl.find('itag=5') > 0:             return vurl     return None 
like image 873
Dave Avatar asked Feb 26 '13 02:02

Dave


Video Answer


1 Answers

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('&amp;')     vurls = urllib2.unquote(grps[0])     videoUrl = None     for vurl in vurls.split('|'):         if vurl.find('itag=5') > 0:             return vurl     return None 
like image 113
Ian McMahon Avatar answered Oct 16 '22 14:10

Ian McMahon