I’m trying to create a simple web bot which will access a certain page, fill in and submit form data, and then print the page which it redirects to upon submission. However my resp.read() call keeps printing the initial form-submission page.
Here’s the page and my code:
<html>
<body>
<P>Welcome <BR><P>Please answer the question <BR><P>98270+88340= ?
<form name="loginform" method="post" action="vote.php">
<input name="sum" type="text" id="sum" />
<input type="submit" name="Submit" value="POST Answer">
</form></body>
import urllib
import urllib2
import lxml.html as lh
#parse the operands
url='hidden'
parr=lh.parse(url).xpath('//p/text()')
elem=parr[2]
op1=int(elem[0:5])
op2=int(elem[6:11])
sum=op1+op2
print sum
sum=str(sum)
#request values
values = {
"sum": sum,
"Submit": "POST Answer"
}
#encode for transmssion
data = urllib.urlencode(values)
request = urllib2.Request(url, data)
resp=urllib2.urlopen(request)
#receive response
print resp.read()
resp.close()
I’m using lxml for the HTML parsing. It returns the correct sum.
Question resolved… here’s the solution for anyone who’d like to see it.
Basically the bot opens the url, parses the page for two operands, then inputs the sum into the form before proceeding to vote on a poll in the following page.