Python ArgumentError:参数--q:在YouTube数据API中存在冲突的选项字符串--q

Python ArgumentError:参数--q:在YouTube数据API中存在冲突的选项字符串--q,python,youtube,google-api,youtube-data-api,Python,Youtube,Google Api,Youtube Data Api,我正在尝试使用python代码中的youtube数据API搜索youtube视频。我遇到参数错误,无法修复此问题。类似的问题已经很少了。但到目前为止,还没有解决这个问题的办法。我怎样才能达到这个结果?我尝试使用youtube数据API的github中给出的相同代码。我在下面的代码中得到了上述错误 from apiclient.discovery import build from apiclient.errors import HttpError from oauth2client.tools

我正在尝试使用python代码中的youtube数据API搜索youtube视频。我遇到参数错误,无法修复此问题。类似的问题已经很少了。但到目前为止,还没有解决这个问题的办法。我怎样才能达到这个结果?我尝试使用youtube数据API的github中给出的相同代码。我在下面的代码中得到了上述错误

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser


# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
#   https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = "API KEY" # added perfectly
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

def youtube_search(options):
  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    developerKey=DEVELOPER_KEY)

  # Call the search.list method to retrieve results matching the specified
  # query term.
  search_response = youtube.search().list(
    q=options.q,
    part="id,snippet",
    maxResults=options.max_results
  ).execute()

  videos = []
  channels = []
  playlists = []

  # Add each result to the appropriate list, and then display the lists of
  # matching videos, channels, and playlists.
  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
      videos.append("%s (%s)" % (search_result["snippet"]["title"],
                                 search_result["id"]["videoId"]))
    elif search_result["id"]["kind"] == "youtube#channel":
      channels.append("%s (%s)" % (search_result["snippet"]["title"],
                                   search_result["id"]["channelId"]))
    elif search_result["id"]["kind"] == "youtube#playlist":
      playlists.append("%s (%s)" % (search_result["snippet"]["title"],
                                    search_result["id"]["playlistId"]))

  print "Videos:\n", "\n".join(videos), "\n"
  print "Channels:\n", "\n".join(channels), "\n"
  print "Playlists:\n", "\n".join(playlists), "\n"


if __name__ == "__main__":
  argparser.add_argument("--q", help="Search term", default="funny videos")
  argparser.add_argument("--max-results", help="Max results", default=25)
  args = argparser.parse_args()


  try:
    youtube_search(args)
  except HttpError, e:
    print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
错误的堆栈跟踪是

ArgumentErrorTraceback (most recent call last)
<ipython-input-6-d78ab4f2931f> in <module>()
     47 
     48 if __name__ == "__main__":
---> 49   argparser.add_argument("--q", help="Search term", default="funny videos")
     50   argparser.add_argument("--max-results", help="Max results", default=25)
     51   args = argparser.parse_args()

C:\Anaconda\lib\argparse.pyc in add_argument(self, *args, **kwargs)
   1306                 raise ValueError("length of metavar tuple does not match nargs")
   1307 
-> 1308         return self._add_action(action)
   1309 
   1310     def add_argument_group(self, *args, **kwargs):

C:\Anaconda\lib\argparse.pyc in _add_action(self, action)
   1680     def _add_action(self, action):
   1681         if action.option_strings:
-> 1682             self._optionals._add_action(action)
   1683         else:
   1684             self._positionals._add_action(action)

C:\Anaconda\lib\argparse.pyc in _add_action(self, action)
   1507 
   1508     def _add_action(self, action):
-> 1509         action = super(_ArgumentGroup, self)._add_action(action)
   1510         self._group_actions.append(action)
   1511         return action

C:\Anaconda\lib\argparse.pyc in _add_action(self, action)
   1320     def _add_action(self, action):
   1321         # resolve any conflicts
-> 1322         self._check_conflict(action)
   1323 
   1324         # add to actions list

C:\Anaconda\lib\argparse.pyc in _check_conflict(self, action)
   1458         if confl_optionals:
   1459             conflict_handler = self._get_handler()
-> 1460             conflict_handler(action, confl_optionals)
   1461 
   1462     def _handle_conflict_error(self, action, conflicting_actions):

C:\Anaconda\lib\argparse.pyc in _handle_conflict_error(self, action, conflicting_actions)
   1465                                      for option_string, action
   1466                                      in conflicting_actions])
-> 1467         raise ArgumentError(action, message % conflict_string)
   1468 
   1469     def _handle_conflict_resolve(self, action, conflicting_actions):

ArgumentError: argument --q: conflicting option string(s): --q
ArgumentErrorTraceback(最后一次调用)
在()
47
48如果名称=“\uuuuu main\uuuuuuu”:
--->49 argparser.add_参数(“--q”,help=“搜索词”,default=“搞笑视频”)
50 argparser.add_参数(“--max results”,help=“max results”,默认值=25)
51 args=argparser.parse_args()
C:\Anaconda\lib\argparse.pyc在add_参数中(self,*args,**kwargs)
1306 raise VALUERROR(“metavar元组的长度与NARG不匹配”)
1307
->1308返回自我添加动作(动作)
1309
1310 def添加参数组(self、*args、**kwargs):
C:\Anaconda\lib\argparse.pyc in\u add\u action(self,action)
1680定义添加动作(自我、动作):
1681如果action.option_字符串:
->1682自选项添加动作(动作)
1683其他:
1684自我位置添加动作(动作)
C:\Anaconda\lib\argparse.pyc in\u add\u action(self,action)
1507
1508定义添加动作(自我、动作):
->1509动作=超级(_ArgumentGroup,self)。_添加动作(动作)
1510自组动作追加(动作)
1511返回操作
C:\Anaconda\lib\argparse.pyc in\u add\u action(self,action)
1320定义添加动作(自我、动作):
1321#解决任何冲突
->1322自我检查与冲突(行动)
1323
1324#添加到行动列表
C:\Anaconda\lib\argparse.pyc中的\u检查\u冲突(self,action)
1458如果冲突选项:
1459冲突处理程序=self.\u获取处理程序()
->1460冲突处理程序(操作、冲突选项)
1461
1462定义处理冲突错误(自我、行动、冲突行动):
C:\Anaconda\lib\argparse.pyc在\u handle\u conflict\u错误中(self、action、conflicting\u actions)
1465用于选项_字符串,操作
1466在相互冲突的行动中])
->1467引发参数错误(操作,消息%conflict\u字符串)
1468
1469定义处理冲突解决(自我、行动、冲突行动):
ArgumentError:参数--q:冲突的选项字符串--q