回溯(最近一次调用最后一次):文件“newdata.py”,第51行,在<module> temp_res.update(i ['statistics'])KeyError:'statistics'

时间:2017-04-08 07:56:44

标签: python api

search_response = youtube.search().list(q=options.q, type='video',
        part='id,snippet', maxResults=options.max_results).execute()

videos = {}

# Add each result to the appropriate list, and then display the lists of
# matching videos.
# Filter out channels, and playlists.

for search_result in search_response.get('items', []):
    if search_result['id']['kind'] == 'youtube#video':

 # videos.append("%s" % (search_result["id"]["videoId"]))

        videos[search_result['id']['videoId']] = search_result['snippet'
                ]['title']

# print "Videos:\n", "\n".join(videos), "\n"

s = ','.join(videos.keys())

videos_list_response = youtube.videos().list(id=s,
        part='id,statistics,snippet').execute()

res = []
for i in videos_list_response['items']:
    tempres = dict(v_id=i['id'], v_title=videos[i['id']])
    tempres.update(i['snippet'])
    tempres.update(i['statistics'])
    res.append(tempres)

请输入视频的搜索字词:football 您的搜索字词是:football Traceback(最近一次调用最后一次):   文件“data_pull.py”,第61行,in     tempres.update(ⅰ[ '统计']) KeyError:'statistics'

需要帮助来解决此错误。从https://www.analyticsvidhya.com/blog/2014/09/mining-youtube-python-social-media-analysis/#comment-126365获取此代码并且无法解决此错误。 此文件在无错误之前运行,但在4-5天后,此错误已弹出而不更改代码。

它很奇怪,但帮助将不胜感激! 谢谢

1 个答案:

答案 0 :(得分:0)

因为有些视频,您无法获得statistics

尝试使用搜索字词= Lac Troi Son Tung MTP ,您会发现this video没有using System; using System.Drawing; using System.IO; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; var _driver = new ChromeDriver(); _driver.Navigate().GoToUrl("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe"); _driver.SwitchTo().Frame("iframeResult"); _driver.SwitchTo().Frame(_driver.FindElement(OpenQA.Selenium.By.XPath("//iframe[@src='https://www.w3schools.com']"))); IWebElement element = _driver.FindElement(OpenQA.Selenium.By.XPath("//a[@class='w3schools-logo']")); Console.WriteLine(element.Location); //Return 0 Console.WriteLine(element.Size); //Return 0 Screenshot sc = ((ITakesScreenshot)_driver).GetScreenshot(); using (var img = Image.FromStream(new MemoryStream(sc.AsByteArray)) as Bitmap) { img?.Clone(new Rectangle(element.Location, element.Size), img.PixelFormat).Save(@"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } 属性。