'NoneType' object has no attribute 'group'
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
You use regex
to match the url, but it can't match, so the result is None
and None
type doesn't have the group
attribute
You should add some code to detect
the result
If it can't match the rule, it should not go on under code
def getVideoUrl(content):
fmtre = re.search('(?<=fmt_url_map=).*', content)
if fmtre is None:
return None # if fmtre is None, it prove there is no match url, and return None to tell the calling function
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