无法使用Python中的POST发送数据

时间:2016-11-07 19:28:21

标签: php python

我正在尝试使用来自StachExchange API的POST发送数据。我不确定这似乎是什么问题。我已经检查了脚本,当我尝试以其他方式发布数据时,它工作正常。问题似乎与python脚本有关。脚本从API获取数据,但似乎没有发布到“generate.php”但仍然是代码:

#!/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("")

有人可以解释一下这个问题吗?

1 个答案:

答案 0 :(得分:1)

没关系! python脚本没有任何问题。实际上我忘了将&#34; $ _ GET&#34; 更改为&#34; $ _ POST&#34; &#39; generate.php < / EM>&#39;我正在测试它。