2013-02-26 125 views
7

有人可以帮我处理这段代码吗?我试图制作一个可以播放视频的python脚本,并且我发现这个文件是下载的Youtube视频。我不完全确定发生了什么,我无法弄清楚这个错误。'NoneType'对象没有属性'group'

错误:

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)

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 
+0

@omouse你想看到我所有的代码?这个问题已经回答了 – David 2013-02-26 02:36:51

+7

我很明显在努力学习,不需要这么关键 – David 2013-02-26 02:46:55

回答

12

的错误是在你的第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 
+0

这个工程。由于某种原因,它现在不能识别URL。时间来解决这个问题... – David 2013-02-26 02:13:53

1

您使用regex相匹配的网址,但它无法比拟的,所以结果是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