Python - Fill and submit a HTML Form

2019-02-25 04:53发布

问题:

I would like to fill in and submit a form on a web page using python. The form I want to interact with has several drop down boxes which are filled using JavaScript. I have looked at the mechanize library but it doesn't handle JavaScript. Can you suggest an alternate library/method for interacting with the form?

Cheers,

Pete

回答1:

  1. Selenium RC or Windmill (http://www.getwindmill.com/)

  2. Examine the form's returned GET or POST values; use urllib2 to submit synthesized requests directly.



回答2:

The form effectively sends information back to the server (or maybe somewhere else) after you press the submit button.

I don't know how you would directly interact with the form, but you could just send the information back to the server in the same way as submitting the form would (without actually submitting it or physically pressing on things).

So for example some forms put the parameters specified in the form (and their values) at the end of the url in the HTTP request once submitting the form. Others put the parameters in the body of the HTTP request. In PHP i know that PHP automatically takes these parameters out for you (into a global array).

In this case you would simply put your own version of the parameters (names/values) in the HTTP request, by looking a what names/values there are in the forms source code.

Although the form may send its information to the server using javascript...

Sorry if that made no sense.