Python 'NoneType' 对象没有属性 'group'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15080078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:16:02  来源:igfitidea点击:

'NoneType' object has no attribute 'group'

pythonyoutubedownload

提问by David

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.

有人可以帮我处理这段代码吗?我正在尝试制作一个可以播放视频的 python 脚本,我找到了这个下载 Youtube 视频的文件。我不完全确定发生了什么,我无法弄清楚这个错误。

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)

(第 66-71 行)

content = resp.read()
videoUrl = getVideoUrl(content)

if videoUrl is not None:
    print('Video URL cannot be found')
    exit(1)

(lines 9-17)

(第 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

采纳答案by Ian McMahon

The error is in your line 11, your re.searchis returning no results, ie None, and then you're trying to call fmtre.groupbut fmtreis None, hence the AttributeError.

错误在您的第 11 行,您re.search没有返回任何结果,即None,然后您尝试调用fmtre.groupfmtreNone,因此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

回答by Tanky Woo

You use regexto match the url, but it can't match, so the result is None

regex用来匹配url,但是匹配不上,所以结果是None

and Nonetype doesn't have the groupattribute

并且None类型没有group属性

You should add some code to detectthe result

您应该detect在结果中添加一些代码

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

回答by Jan

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

只是想在这种情况下提及新的walrus运营商,因为这个问题经常被标记为重复,运营商可以很容易地解决这个问题。



Python 3.8Python 3.8我们需要之前:

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

As of Python 3.8we can write the same as:

由于Python 3.8我们可以写一样:

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

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

其他语言以前有这个(想想CPHP),但我认为它可以使代码更清晰。



对于上面的代码,这可能是

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

回答by whitehatceo

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.

只是想补充一下答案,一组数据应该是一个序列,所以你可以匹配分组数据的每个部分,而不需要跳过一个数据,因为如果从一个句子中跳过一个词,我们可能不会参考句子不再作为一组,请参阅下面的示例以获取更多说明,但是,不推荐使用 compile 方法。

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.