forms - Sending POST request on an already open url in python -


basically want send post request following form.

<form method="post" action=""> 449 * 803 - 433 * 406 = <input size=6 type="text" name="answer" /> <input type="submit" name="submitbtn" value="submit" /> </form> 

what want read through page, find out equation in form, calculate answer, enter answer parameter send post request, without opening new url page, new equation comes every time page opened, hence obtained result becomes obsolete. want obtain page comes result of sending post request. i'm stuck @ part have send post request without opening new url instance. also, appreciate on how read through page again after post request. (would calling read() suffice?)

the python code have looks this.

import urllib, urllib2  link = "http://www.websitetoaccess.com" f = urllib2.urlopen(link)  line = f.readline().strip() equation = '' result = '' file1 = open ('firstpage.html' , 'w') file2 = open ('finalpage.html', 'w')  line in f:     if 'name="answer"' in line:         result = getresult(line)     file1.write(line)  file1.close()  raw_params = {'answer': str(result), 'submit': 'submit'} params = urllib.urlencode(raw_params) request = urllib2.request(link, params) page = urllib2.urlopen(request)  file2.write(page.read()) file2.close() 

i'm bit puzzled, post request new separate request don't understand mean "without opening new url instance" ... have tried taking @ happens when you're trying in script manually? open developer console in chrome, go network tab, toggle preserve log on, delete history, , you're trying manually? replicate in python? recommend try out requests module, makes things simpler using urllib. pip install requests (and pip install lxml).

import requests lxml import etree  url = 'http://www.websitetoaccess.com' res1 = requests.get(url) # res1.content # try parsing html page lxml root = etree.fromstring(res1.content, etree.htmlparser()) # root, find question , calc answer? post_params = {'answer': str(42), 'submit': 'submit'} res2 = requests.post(url, data=post_params) # check res2 success or content? 

edit:

you're possibly experiencing header issue or cookies issue. might receiving session id enables server determine question received in previous request. post request separate request previous request, can't combined 1 single request. should check headers received previous request and/or try setting session/cookies handling (easy if using requests, see http://docs.python-requests.org/en/master/user/advanced/).


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -