15

I am trying / wanting to write a Python script (2.7) that goes to a form on a website (with the name "form1") and fills in the first input-field in said form with the word hello, the second input-field with the word Ronald, and the third field with [email protected]

Can anyone help me code or give me any tips or pointers on how to do this ?

2 Answers 2

19

Aside from Mechanize and Selenium David has mentioned, it can also be achieved with Requests and BeautifulSoup.

To be more clear, use Requests to send request to and retrieve responses from server, and use BeautifulSoup to parse the response html to know what parameters to send to the server.

Here is an example script I wrote that uses both Requests and BeautifulSoup to submit username and password to login to wikipedia:

import requests
from bs4 import BeautifulSoup as bs


def get_login_token(raw_resp):
    soup = bs(raw_resp.text, 'lxml')
    token = [n['value'] for n in soup.find_all('input')
             if n['name'] == 'wpLoginToken']
    return token[0]

payload = {
    'wpName': 'my_username',
    'wpPassword': 'my_password',
    'wpLoginAttempt': 'Log in',
    #'wpLoginToken': '',
    }

with requests.session() as s:
    resp = s.get('http://en.wikipedia.org/w/index.php?title=Special:UserLogin')
    payload['wpLoginToken'] = get_login_token(resp)

    response_post = s.post('http://en.wikipedia.org/w/index.php?title=Special:UserLogin&action=submitlogin&type=login',
                           data=payload)
    response = s.get('http://en.wikipedia.org/wiki/Special:Watchlist')

Update:

For your specific case, here is the working code:

import requests
from bs4 import BeautifulSoup as bs


def get_session_id(raw_resp):
    soup = bs(raw_resp.text, 'lxml')
    token = soup.find_all('input', {'name':'survey_session_id'})[0]['value']
    return token

payload = {
    'f213054909': 'o213118718',  # 21st checkbox
    'f213054910': 'Ronald',  # first input-field
    'f213054911': '[email protected]',
    }

url = r'https://app.e2ma.net/app2/survey/39047/213008231/f2e46b57c8/?v=a'

with requests.session() as s:
    resp = s.get(url)
    payload['survey_session_id'] = get_session_id(resp)
    response_post = s.post(url, data=payload)
    print response_post.text
Sign up to request clarification or add additional context in comments.

6 Comments

Ah, okay. I can kind of see how this works, but I am confused on how to do some things still.
Would you mind showing me how to achieve the following instance of filling a form, that way I can learn how to fill out the form I am trying to? The form is located at app.e2ma.net/app2/survey/39047/213008231/f2e46b57c8/?v=a
I am trying to fill the form with the id of " survey_form ". Then I want to tick the 21st check-box, which has an id of " option_213118718 ". Second, I want to fill the first input-field, with an id of " field_213054910_input " and a name of " f213054910 ", with the text ' Ronald '.
and the next input-field with an id of ' field_213054911_input ' and name of ' f213054911 '. I want to fill that with the text ' [email protected] ' and then submit the form. I would really appreciate any and all help in solving this.
@IrfanM I've updated with the working code. Bare in mind that it's still worth learning Mechanize and Selenium too.
|
6

Take a look at Mechanize and Selenium. Both are excellent pieces of software that would allow you to automate filling and submitting a form, among other browser tasks.

2 Comments

I've heard of Mechanize before, but I am confused on how to do what I am trying to accomplish. More specifically actually filling out the form with those lines. Is there any way you could give or show me some basic code to start off from?
Mechanize comes with tons of example scripts. There's a whole section devoted to forms that should help you get started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.