1

I am trying to send data using POST from StachExchange API. I am not sure what seems to be the problem. I have checked the script, it's working fine when I try to POST data some other way. The problem seems to be with the python script. The scripts gets the data from the API but doesn't seems to be posting to "generate.php" Nonetheless here's the code:

#!/usr/bin/env python
import requests, json

userinput = input('Enter a keyword: ')
userinputq = input('Enter page: ')

getparams = {'page':userinputq, 'pagesize':'100', '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']:

     if looping['is_answered'] == True:
       try:
          newparams = {'order':'desc', 'sort':'votes', 'site':'stackoverflow', 'filter': '!4(Yrwr)RRK6oy2JSD'}
          newr = requests.get('https://api.stackexchange.com/2.2/answers/'+str(looping['accepted_answer_id']), params=newparams) 
          newresult = json.loads(newr.text)
          titletopost = 'Title:', looping['title']
          bodytopost = '<h1>Question:</h1><br>'+(looping['body'])+'<br>'+'Link to Question: '+(looping['link'])+'<br><br><br>'+'<h1>Answer:</h1><br>'+(newresult['items'][0]['body'])
          enterremove = bodytopost.replace('\n', '').replace('\r', '')
          print(enterremove)
          userdata = {"secret":"Secret", "topic_title":titletopost, "body":enterremove}
          requests.post("http://www.example.com/generate.php", data=userdata)
       except KeyError: print("No answer ID found.")         

print("")
print("")

Can anyone please explain the problem?

17
  • 1
    You may want to fix the indent on the print() on the first if statement Commented Nov 7, 2016 at 19:30
  • It also says that requests does not exist Commented Nov 7, 2016 at 19:30
  • Yeah, thanks, fixed! And what requests? The import library? You can install it using: pip install requests Commented Nov 7, 2016 at 19:37
  • Where do I type that? I am on Windows 10 Commented Nov 7, 2016 at 19:38
  • And yes, I am talking about the import requests, json Commented Nov 7, 2016 at 19:39

1 Answer 1

2

Nevermind! There's nothing wrong with the python script. Actually I forgot to change "$_GET" to "$_POST" in 'generate.php' while I was testing it.

Sign up to request clarification or add additional context in comments.

1 Comment

Haha the code did look ok to me :D Glad it is fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.