【问题标题】:Python 2.7 TypeError: 'NoneType' object has no attribute '_getitem_'Python 2.7 TypeError:“NoneType”对象没有属性“__getitem__”
【发布时间】:2017-01-08 19:47:21
【问题描述】:

我对编码很陌生,并且一直在尝试一些事情。当我运行我拥有的 python 脚本时出现此错误。我已经读到这个错误是因为某些东西正在返回“无”,但我无法弄清楚是什么原因导致它(仍在尝试了解所有这些)。

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

    [*] 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_'

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

import argparse
import requests
import json

from pytineye import TinEyeAPIRequest

tineye = TinEyeAPIRequest('http://api.tineye.com/rest/','PUBLICKEY','PRIVATEKEY')

youtube_key = "VIDEOID"

ap = argparse.ArgumentParser()
ap.add_argument("-v","--videoID",    required=True,help="The videoID of the YouTube video. For example: https://www.youtube.com/watch?v=VIDEOID")
args = vars(ap.parse_args())

video_id    = args['videoID']

#
# Retrieve the video details based on videoID
#
def youtube_video_details(video_id):

    api_url  = "https://www.googleapis.com/youtube/v3/videos?part=snippet%2CrecordingDetails&"
    api_url += "id=%s&" % video_id
    api_url += "key=%s" % youtube_key

    response = requests.get(api_url)

    if response.status_code == 200:

        results = json.loads(response.content)

        return results

    return None


print "[*] Retrieving video ID: %s" % video_id
video_data = youtube_video_details(video_id)

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

print "[*] Thumbnails retrieved. Now submitting to TinEye."

url_list = []

# add the thumbnails from the API to the list
for thumbnail in thumbnails:

    url_list.append(thumbnails[thumbnail]['url'])


# build the manual URLS
for count in range(4):

    url = "http://img.youtube.com/vi/%s/%d.jpg" % (video_id,count)

    url_list.append(url)


results = []

# now walk over the list of URLs and search TinEye
for url in url_list:

    print "[*] Searching TinEye for: %s" % url

    try:
        result = tineye.search_url(url)
    except:
        pass

    if result.total_results:
        results.extend(result.matches)

result_urls = []
dates       = {}

for match in results:

    for link in match.backlinks:

        if link.url not in result_urls:

            result_urls.append(link.url)
            dates[link.crawl_date] = link.url

print            
print "[*] Discovered %d unique URLs with image matches." % len(result_urls)

for url in result_urls:

    print url


oldest_date = sorted(dates.keys())

print
print "[*] Oldest match was crawled on %s at %s" % (str(oldest_date[0]),dates[oldest_date[0]])

我知道这可能很简单,但我似乎终其一生都无法弄清楚。任何帮助将不胜感激。

【问题讨论】:

  • 错误是尝试索引video_data时,video_data是从youtube_video_details获取的。看看,如果status_code != 200youtube_video_details 会返回什么?
  • 感谢您的快速回复。试过了,得到了一个 KeyError。下班后会更深入地研究它。

标签: python python-2.7 typeerror


【解决方案1】:

在您的 youtube_video_details 方法中。 response.status_code 可能不是200,所以方法返回None

所以你可以这样做:

video_data = youtubo_video_details(video_id)

if not video_data:        

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

【讨论】:

  • 感谢您的回复。下班我会试试看。
猜你喜欢
  • 2018-04-13
  • 2013-04-15
  • 1970-01-01
  • 2017-08-17
  • 2012-12-04
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
相关资源
最近更新 更多