WebForm Submission with Python

Shashank Shanu

a year ago

WebForm Submission in Python
WebForm Submission in Python
Sometime we need to interact with a webpage which needs some data to be submitted to the server through the forms present in the html page.
These types of webpages are mainly used for signing up for a new account or supplying some information like name, registration number and mobile number etc. to retrieve the result of an examination.
Python provides a module requests which helps us to handle this type of requirement gracefully using the POST method with some others required parameters.
Let’s take an example to understand it in better way.

Example

In this example, we are trying to sign up into a website and here we are providing user id and password. Then after filling all the details we are printing the response what we received from the website.
import requests
USERNAME_ID = 'signup-user-name'
PASSWORD_ID = 'signup-user-password'
USERNAME = 'username'
PASSWORD = 'userpassword'
SIGNUP_URL = 'https://codepad.org/login'
def submit_form():
    """Submit a form"""
    payload = {ID_USERNAME : USERNAME, ID_PASSWORD : PASSWORD,}
    resp = requests.get(SIGNUP_URL)
    print("Response to GET request: %s" %resp.content)
    resp = requests.post(SIGNUP_URL, payload)
    print("Headers from a POST request response: %s" %resp.headers)
#print "HTML Response: %s" %resp.read()

if __name__ == '__main__':
    submit_form()
When we run the above program, we get the below mentioned response from the webpage−
Response to GET request: <!DOCTYPE html>
<html>

<head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <meta HTTP-EQUIV="Expires" CONTENT="-1">

    <title>Login - codepad</title>

    

    <link href="/main.css" media="screen" rel="stylesheet" type="text/css" />
    <style type="text/css">
    

    </style>
    <script src='https://www.google.com/recaptcha/api.js'></script>
    <script>
       function onRecaptcha(token) {
         document.getElementById("editor-form").submit();
       }
    </script>
</head>


    <body >

	.....................
	.....................
I hope you enjoyed reading this article and finally, you came to know about WebForm Submission with Python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review