无法使用python请求提交表单

时间:2017-09-28 15:26:05

标签: python forms python-requests

我一直在尝试提交表单请求但不能。我试图使用Request库发送。我无法弄清楚要发送的参数。 只有一个textarea和提交按钮。 以下是网站来源:

<form class="form-horizontal">
<input id="RId" type="hidden" value="Some_XYZ_Code" />
  <div class="row">
  <div class="col-md-12">
  <div id="TextContainer" class="textarea-container">
  <textarea id="Text" rows="5" maxlength="700" class="abc"></textarea>
  </div>
  </div>
  <div class="col-md-12">
  <button id="Send" class="btn btn-primary-outline" type="button"   onclick="SendMsg()" data-loading-text="Loading..."><span class="icon icon-pencil"></span> Send</button>
  </div>
  </div>
  </form>

我的代码如下:

import requests
r = requests.post("https://akshitdhar.sarahah.com/",
              data={'id':'RId','value':'Some_XYZ_Code','Text':'abc'})
print(r.status_code)

1 个答案:

答案 0 :(得分:1)

向Sarahah发送消息:

def makePost2User(userId, msg):
    r = requests.get("https://%s.sarahah.com/" % userId);

    if (r.status_code != 200):
        print("Error making GET Request. Server returned Code: %s" % r.status_code)
        pass

    cookie = r.headers['Set-Cookie'].split(";")[0]
    print("Cookie: %s" % cookie)
    headers = { 'Cookie': cookie }

    tstr = r.content.decode().split("<input id=\"RecipientId\" type=\"hidden\" value=\"")[1]
    uId = tstr.split("\"")[0]
    print("UserID: %s" % uId)

    tstr = r.content.decode().split("name=\"__RequestVerificationToken\" type=\"hidden\" value=\"")[1]
    rToken = tstr.split("\"")[0]
    print("Token: %s" % rToken)

    url = "https://%s.sarahah.com/Messages/SendMessage" % userId
    data = { "__RequestVerificationToken":rToken, "userId": uId, "text": msg }

    p = requests.post(url, headers=headers, data=data)
    print("Posted. Server returned Code: %s" % p.status_code)
    pass