Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

'NoneType' object has no attribute 'group'

Writer Andrew Henderson

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
2

4 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
1

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

just wanted to add to the answers, a group of data is expected to be in a sequence, so you can match each section of the grouped data without skipping over a data because if a word is skipped from a sentence, we may not refer to the sentence as one group anymore, see the below example for more clarification, however, the compile method is deprecated.

msg = "Malcolm reads lots of books"
#The below code will return an error.
book = re.compile('lots books')
book = re.search(book, msg)
print (book.group(0))
#The below codes works as expected
book = re.compile ('of books')
book = re.search(book, msg)
print (book.group(0))
#Understanding this concept will help in your further
#researchers. Cheers.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy