user41992 user41992 - 3 months ago 19
Python Question

get() takes exactly 1 argument (3 given)

#!/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 ''


I am trying to make a request such as "https://api.stackexchange.com/2.2/answers/12345 (User inputs 12345)" but I don't know how to do that. And if I include a string it returns error. Help, please?

I am getting this error:

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)

Answer

update the request line as

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

you need to concatenate the looping['accepeted answer']

Comments