'NoneType' object has no attribute 'group' 'NoneType' object has no attribute 'group' python python

'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


Just wanted to mention the newly walrus operator in this context because this question is marked as a duplicate quite often and the operator may solve this very easily.


Before Python 3.8 we needed:

match = re.search(pattern, string, flags)if match:    # do sth. useful here

As of Python 3.8 we can write the same as:

if (match := re.search(pattern, string, flags)) is not None:    # do sth. with match

Other languages had this before (think of C or PHP) but imo it makes for a cleaner code.


For the above code this could be

def getVideoUrl(content):    if (fmtre := re.search('(?<=fmt_url_map=).*', content)) is None:        return None    ...