python - 'NoneType' 对象没有属性 'group'

标签 python youtube download

有人可以帮我处理这段代码吗?我正在尝试制作一个可以播放视频的 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;')

代码片段:

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

你可以试试:

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

关于python - 'NoneType' 对象没有属性 'group',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080078/

相关文章:

python - numpy.log 中的 "RuntimeWarning: divide by zero encountered in log"即使小值被过滤掉

youtube - youtube视频中的CPN是什么

android - 1-2 秒后暂停 youtube 视频

ruby-on-rails - 未定义的局部变量或方法 - 下载文件 - Ruby on Rails

c# - 在 C# 中使用 Post 下载附件文件

python - 为返回对象层次结构的函数编写单元测试

python - 如何使用自引用和带有插槽的类来 pickle 和取消 pickle 对象?

python - 文本挖掘和 NLP : from R to Python

.net - Google API YouTube示例-错误:redirect_uri_mismatch

Ios 通过浏览器或其他带有 url 的应用程序上传和下载 .app 文件