Python 2.7 TypeError:' NoneType'对象没有属性' _getitem _'

时间:2016-09-01 04:45:15

标签: python python-2.7 typeerror

我对编码很陌生并且一直在尝试一些事情。我运行python脚本时收到此错误。我已经读过这个错误是因为有些东西正在返回"无"但是我很难搞清楚导致它的原因(仍在努力学习所有这些)。

脚本的目的是从视频中提取缩略图并在互联网上搜索同一事物的其他实例。运行我的python脚本后,它返回结果:

// these classes cannot be modified and do not have source available

class Legacy {
}

class Legacy1 extends Legacy {
}
class Legacy2 extends Legacy {
}

// this is the implementation of your visitor    

abstract class LegacyVisitor<T> {
    abstract T visitLegacy1(Legacy1 l);
    abstract T visitLegacy2(Legacy2 l);

    T accept(Legacy l) {
        if (l instanceof Legacy1) {
            return visitLegacy1((Legacy1)l);
        } else if (l instanceof Legacy2) {
            return visitLegacy2((Legacy2)l);
        } else {
            throw new RuntimeException("Unknown concrete Legacy subclass:" + l.getClass());
        }
    }
}
public class Test {
    public static void main(String[] args) {
        String s = new LegacyVisitor<String>() {

            @Override
            String visitLegacy1(Legacy1 l) {
                return "It's a 1";
            }

            @Override
            String visitLegacy2(Legacy2 l) {
                return "It's a 2";
            }
        }.accept(new Legacy1());

        System.out.println(s);
    }
}

以下是我正在运行的脚本(已删除Youtube Key):

    [*] Retrieving video ID: VIDEOID
    Traceback (most recent call last):
    File "VidSearch.py", line 40, in <module>
    thumbnails = video_data['items'][0]['snippet']['thumbnails']
    TypeError: 'NoneType' object has no atribute '_getitem_'

我知道它可能很简单,但我似乎无法想象我的生活。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

In your youtube_video_details method. the response.status_code maybe is not 200, so the method return the None

So you can do like this:

video_data = youtubo_video_details(video_id)

if not video_data:        

    thumbnails = video_data['items'][0]['snippet']['thumbnails']