2016-08-23 74 views
-4
#!/usr/bin/env python 
import requests, json 

userinput = raw_input('Enter a keyword: ') 

getparams = {'order':'desc', 'sort':'votes', 'intitle':userinput, 'site':'stackoverflow', 'filter': '!5-HwXhXgkSnzI0yfp0WqsC_-6BehEi(fRTZ7eg'} 

r = requests.get('https://api.stackexchange.com/2.2/search', params=getparams) 

result = json.loads(r.text) 

if result['has_more'] == False: 
print 'Error given.' 
else: 
for looping in result['items']: 
    print '' 
    print '' 
    print 'Title:', looping['title'] 
    #print 'Question:', looping['body'] 
    print 'Link:', looping['link'] 
    if looping['is_answered'] == True: 
     try: 
      print 'Answer ID#:', looping['accepted_answer_id'] 
      newparams = {'order':'desc', 'sort':'votes', 'site':'stackoverflow', 'filter': '!4(Yrwr)RRK6oy2JSD'} 
      newr = requests.get('https://api.stackexchange.com/2.2/answers/', looping['accepted_answer_id'], params=newparams) 
      newresult = json.loads(newr.text) 
      print newresult['items'][0]['body'] 
      except KeyError: print 'No answer ID found.' 
    print '' 
    print '' 

我試圖讓諸如「https://api.stackexchange.com/2.2/answers/12345(用戶輸入12345)」的要求,但我不知道該怎麼做。如果我包含一個字符串,它會返回錯誤。請幫助?的get()恰恰1參數(3給出)

我收到此錯誤:

Enter a keyword: php 


Title: How can I prevent SQL-injection in PHP? 
Link: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php 
Answer ID#: 60496 
Traceback (most recent call last): 
File "./warrior.py", line 25, in <module> 
newr = requests.get('https://api.stackexchange.com/2.2/answers/', looping['accepted_answer_id'], params=newparams) 
TypeError: get() takes exactly 1 argument (3 given) 
+1

您需要提供一個[ MCVE。現在,我們無法幫助,因爲我們看不到你在做什麼。 –

+1

你可以顯示你的代碼的其餘部分? – Harrison

+0

我已經更新了代碼。對於那個很抱歉。 – user41992

回答

0

更新請求行

newr = requests.get('https://api.stackexchange.com/2.2/answers/'+str(looping['accepted_answer_id']), params=newparams) 

您需要連接成環[ 'accepeted回答']

+0

非常感謝。有效! :d – user41992

相關問題