【问题标题】:'NoneType' object has no attribute 'group'“NoneType”对象没有属性“组”
【发布时间】:2013-02-11 08:53:26
【问题描述】:

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

错误:

AttributeError: 'NoneType' object has no attribute 'group'

追溯:

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;')

代码sn-p:

(第 66-71 行)

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

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

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

【问题讨论】:

  • @omouse 你想看我所有的代码吗?问题已回答
  • 我明明是在努力学习,没必要这么挑剔

标签: python youtube download


【解决方案1】:

错误在您的第 11 行,您的 re.search 没有返回任何结果,即 None,然后您尝试调用 fmtre.groupfmtreNone,因此是 AttributeError .

你可以试试:

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

【讨论】:

  • 这行得通。由于某种原因,它现在无法识别 URL。是时候弄清楚了……
【解决方案2】:

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

None 类型没有group 属性

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

如果不能匹配到规则,就不应该在代码下进行

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

【讨论】:

    【解决方案3】:

    只想在此上下文中提及新的walrus operator,因为此问题经常被标记为重复,操作员可以很容易地解决这个问题。


    Python 3.8 之前,我们需要:
    match = re.search(pattern, string, flags)
    if match:
        # do sth. useful here
    

    截至Python 3.8,我们可以这样写:

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

    其他语言以前也有这种情况(想想CPHP),但 imo 它使代码更简洁。


    对于上面的代码,这可能是
    def getVideoUrl(content):
        if (fmtre := re.search('(?<=fmt_url_map=).*', content)) is None:
            return None
        ...
    

    【讨论】:

      【解决方案4】:

      只是想添加到答案中,一组 的数据应按顺序排列,因此您可以 匹配分组数据的每个部分 跳过数据,因为如果从 句子,我们可能不再将句子称为一组,请参阅下面的示例以获得更多说明,但是不推荐使用 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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-15
        • 2015-09-06
        • 1970-01-01
        相关资源
        最近更新 更多