By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

Automated form submission

Thekid
100+
P: 145
I'm trying to auto send a form submission to a website but it isn't working. I've done this before and it worked but for some reason, using the same basic code, it doesn't seem to submit it. I'm using urllib & urllib2 to open the page and re to parse the info. On the page is a text entry field and a 'submit' button. I've compared the source code of both pages and they appear the same as far as the submission goes. Anything else I can try? I don't want to bother with Beautiful Soup right now because I'm very close with what I have. On the page is:
Answer:
'submit button'

What I've used:
Expand|Select|Wrap|Line Numbers
  1. import urllib2
  2. import urllib
  3. import re
  4.  
  5. url='http://website.here'
  6. strSession='cookie.info.here'
  7. dicHeaders={'COOKIE':strSession}
  8. req=urllib2.Request(url, None, dicHeaders)
  9. f=urllib2.urlopen(req)
  10. strContext=f.read()
  11. out=open("data.txt","w")
  12. out.write(strContext)
  13. out.close()
  14. infile=open("data.txt","r")
  15.  
  16. *re code here*
  17.  
  18.         answer=10 # answer varies
  19.         print answer
  20.         password=urllib.urlencode({'solution':answer})
  21.         req2=urllib2.Request(url,password,dicHeaders)
  22.         f2=urllib2.urlopen(req2)
  23.         print f2.read()
  24.  
  25.  
If I test it like this on the previous page that it worked on, I get the response that says "Sorry, your answer is wrong" which is fine because I know it submitted the answer but on the current page (which is very similar) it doesn't seem to submit it.
Here are the form actions of both pages for comparison. The first is the one that previously worked, the 2nd the new one I'm trying.

************************************************** ******************************************
<form name="submitform" action="/website.here" method="post"><br />
Answer:
<input size="50" name="solution"><br /><br />
<div style="text-align:center">
<input name ="submitbutton" type="submit" value="submit (remaining time: 3 seconds)">
</div>
</form>
************************************************** *******************************************
<form name="submitform" action="/website.here" method="POST"><br />
Answer: <br />
<input size="75" name="solution"><br /><br />
<div style="text-align:center">
<input name ="submitbutton" type="submit" value="Submit (remaining time: 5 seconds)">
</div>

</form>
************************************************** *******************************************

Is there another way for me to get this answer to send?
Feb 5 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.