'NoneType' 对象没有属性 'group' [英] 'NoneType' object has no attribute 'group'

查看:26
本文介绍了'NoneType' 对象没有属性 'group'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

错误:

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

代码片段:

(第 66-71 行)

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

推荐答案

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

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.

你可以试试:

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

这篇关于'NoneType' 对象没有属性 'group'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆