python - 在Python中使用gdata Youtube API时出现套接字错误

标签 python youtube youtube-api gdata

我正在使用gdata通过以下代码将YouTube URL映射到视频标题:

import gdata.youtube.service as youtube
import re
import queue
import urlparse

ytservice = youtube.YouTubeService()
ytservice.ssl = True
ytservice.developer_key = '' # snip

class youtube(mediaplugin):
  def __init__(self, parsed_url):
    self.url = parsed_url
    self.video_id = urlparse.parse_qs(parsed_url.query)['v'][0]
    self.ytdata = ytservice.GetYouTubeVideoEntry(self.video_id)
    print self.ytdata

调用service.GetYouTubeVideoEntry()时收到以下套接字异常:
  File "/Users/haldean/Documents/qpi/qpi/media.py", line 21, in __init__
    self.ytdata = ytservice.GetYouTubeVideoEntry(self.video_id)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/gdata/youtube/service.py", line 210, in GetYouTubeVideoEntry
    return self.Get(uri, converter=gdata.youtube.YouTubeVideoEntryFromString)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/gdata/service.py", line 1069, in Get
    headers=extra_headers)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/atom/__init__.py", line 93, in optional_warn_function
    return f(*args, **kwargs)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/atom/service.py", line 186, in request
    data=data, headers=all_headers)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/atom/http_interface.py", line 148, in perform_request
    return http_client.request(operation, url, data=data, headers=headers)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/atom/http.py", line 163, in request
    connection.endheaders()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 937, in endheaders
    self._send_output(message_body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 797, in _send_output
    self.send(msg)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 759, in send
    self.connect()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1140, in connect
    self.timeout, self.source_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 8] nodename nor servname provided, or not known  

我不知道如何开始调试它。任何想法表示赞赏。谢谢!

编辑:
为了回应评论中提出的问题,video_idqh-mwjF-OMoparsed_url是:
ParseResult(scheme=u'http', netloc=u'www.youtube.com', path=u'/watch', params='', query=u'v=qh-mwjF-OMo&feature=g-user-u', fragment='')

最佳答案

我的错误是video_id应该作为关键字参数传递,如下所示:

    self.ytdata = ytservice.GetYouTubeVideoEntry(video_id=self.video_id)

套接字异常似乎是gdata唯一会抛出异常的层;它会尝试根据参数盲目获取URL,并且只有在URL提取失败时才会失败。

关于python - 在Python中使用gdata Youtube API时出现套接字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11814571/

相关文章:

python - 没有预测样本的标签中出现警告 : Precision and F-score are ill-defined and being set to 0. 0。使用 `zero_division` 参数

twitter-bootstrap - 嵌入式视频显示了另一个视频

python - 关闭线程循环

python - 如何在由类对象组成的列表中查找特定值的最大元素索引?

youtube - 获取 YouTube 直播事件的当前时长

iphone - UIWebView 流媒体 YouTube 在 iPad 上无法全屏显示

jquery - 使用 Youtube Google API 和 jQuery

youtube - 如何以 XML 而不是 JSON 的形式获取 youtube API 3 响应?

google-api - Youtube V3 API 配额使用帮助

python - 使用python和RSA算法的加密聊天应用程序