第二次执行后的子程序错误

时间:2015-06-03 13:14:00

标签: python-3.x youtube-api

我正在使用YouTube API搜索视频,获取一些信息,例如channelID,videoID等,并将信息添加到表格中。子程序由来自按钮点击的信号和来自行编辑的搜索输入触发。

以下是代码:

def searchSend(self):
    search = self.ui.YoutubeSearch.text()
    self.ui.requestView.setRowCount(0)
    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()

        VideoID = []
        ChannelID = []
        VideoName = []
        ChannelName = []

        # 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":
                VideoID.append(search_result["id"]["videoId"])
                ChannelID.append(search_result["snippet"]["channelId"])
                VideoName.append(search_result["snippet"]["title"])
                ChannelName.append(search_result["snippet"]["channelTitle"])

        for item in VideoID:
            rowCount = self.ui.requestView.rowCount()
            self.ui.requestView.insertRow(rowCount)
            self.ui.requestView.setItem(rowCount,0,QtGui.QTableWidgetItem(VideoName[rowCount]))
            self.ui.requestView.setItem(rowCount,1,QtGui.QTableWidgetItem(ChannelName[rowCount]))
            self.ui.requestView.setItem(rowCount,2,QtGui.QTableWidgetItem(VideoID[rowCount]))


    argparser.add_argument("--q", default=str(search))
    argparser.add_argument("--max-results", default=15)
    args = argparser.parse_args()

    try:
        youtube_search(args)
    except HttpError:
        print ("An HTTP error %d occurred:\n%s") % (e.resp.status, e.content)

以下是第二次运行子程序时的错误:

Traceback (most recent call last):
  File "D:\My Documents\Request\mainwindow.py", line 112, in searchSend
    argparser.add_argument("--q", default=str(search))
  File "C:\Python33\lib\argparse.py", line 1326, in add_argument
    return self._add_action(action)
  File "C:\Python33\lib\argparse.py", line 1686, in _add_action
    self._optionals._add_action(action)
  File "C:\Python33\lib\argparse.py", line 1530, in _add_action
    action = super(_ArgumentGroup, self)._add_action(action)
  File "C:\Python33\lib\argparse.py", line 1340, in _add_action
    self._check_conflict(action)
  File "C:\Python33\lib\argparse.py", line 1479, in _check_conflict
    conflict_handler(action, confl_optionals)
  File "C:\Python33\lib\argparse.py", line 1488, in _handle_conflict_error
    raise ArgumentError(action, message % conflict_string)
argparse.ArgumentError: argument --q: conflicting option string: --q

由于

1 个答案:

答案 0 :(得分:0)

我认为问题在于,当再次运行子过程时,argparser已经添加了参数并将其存储。

您不能再次添加参数,因为它与已存储的参数冲突。

相反,我返回了args值并编辑了' q'其中的价值。