Reputation: 937
I have the following details about a POST request
1)The URL: "http://kuexams.org/get_results"
2)Request body: "htno=001111505&ecode=kuBA3_Supply_Dec_2013".
I got this from analyzing HTTP traffic. Then I found this site where you specify these values and it reurns the response. https://requestable.pieterhordijk.com/sSd7o
I need to know how to make something similar to what the site does. Just post the Request body to the URL and return the data for parsing.
P.S: I've tried multiple methods for POST to this site http://kuexams.org/results/3GKZ-D_QBHLWXrg7lZ2IGoKBI7lGfpSK37GNoykJ8k5UerNGYn21FN6w_R5XZ8IQVUHRb8ZYVwq-zN4BhIjusQ,,/ugresults/ug Here is what I tired. It uses a different method but fails miserably.
import mechanize
import cookielib
import urllib
import logging
import sys
import re
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=3)
br.addheaders = [('User-agent', 'Firefox')]
r = br.open('http://kuexams.org/results/06JZ4SYmTV97s4oROGuLYglPFH3XxJKAunIilJkDBV0gBxSU6YVJ_kXRL0UZb3cIjz9aFdnkYaE-T_S3ubaXPg,,/ugresults/ug')
scrape = r.read()
#print(scrape)
pattern = re.compile('=5&code=(.{5})\'')
img = pattern.findall(scrape)
print(img)
imgstr = str()
imgstr = img[0]
print(imgstr)
br.select_form("appForm")
br.form["htno"] ='001111441'
br.form["entered_captcha"] = imgstr
response = br.submit()
print response.read()
br.back()
Upvotes: 0
Views: 1268
Reputation: 5728
I can see post request there not using any cookie or captcha details. Its implemented only client side, So no need for storing cookie or getting captcha therefore this code will work for you. Use requests its cool and easy to use.
import requests
url='http://kuexams.org/get_results'
payload={'htno': '001111441', 'ecode': 'kuBA2_Supply_Dec_2013'}
headers={"User-Agent": "Some Cool Thing"}
r=requests.post(url,headers=headers,data=payload)
print r.content
Note that server is not accepting default User-agent by requests so i added custom one without that it won't accept POST request or might give Forbidden Error.
Upvotes: 2