2015-06-03 29 views
0

我正在使用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 

感謝

回答

0

我認爲這個問題是在運行時子程序再次,參數已經被argparser添加並被存儲。

您不能再次添加參數,因爲它與已存儲的參數衝突。

取而代之,我返回了參數值並在其中編輯了'q'值。

相關問題