有人可以帮我处理这段代码吗?我正在尝试制作一个可以播放视频的 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('&')
代码片段:
(第 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('&')
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。 group
但 fmtre
是 None
,因此出现 AttributeError
。
你可以试试:
def getVideoUrl(content):
fmtre = re.search('(?<=fmt_url_map=).*', content)
if fmtre is None:
return None
grps = fmtre.group(0).split('&')
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/