How to go to next page on google form using requests.post

Viewed 504

I've looked at multiple tutorials on how to fill out a google form and have successfully semi accomplished it. My problem is that the google form has 2 pages before you submit it.

I've created my form data:

    form_data = {
    'entry.1019016807': 'My name',
    'draftResponse': [],
    'pageHistory': 0
    }

and have the post

    user_agent = {
    'Referer': 'https://docs.google.com/forms/d/e/1FAIpQLSfktx3zRs4rqaZMNBc17oFuHQOJ1ckHz1lyYaN1kzaNCq9uyQ/formResponse',
    'User-Agent': "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36"}
    requests.post(url, data=form_data, headers=user_agent)

Changing page history to 1 fills out data on the second page and setting it to 0 fills out data in the first page. I've tried using 2 requests.post with different page history but it just creates 2 separate google form responses. There is also more form data but I didn't include it. All entry.id's are correct.

1 Answers

try this code. I've comment it to understand how it works.

import requests
from bs4 import BeautifulSoup as bs4

# First, download the form and parse it with beautifulsoup :
url = 'https://forms.gle/8nt88S9jc5zNDmqM8'
response = requests.get(url)
html = bs4(response.text, 'html.parser')

# the balise <form action="url_to_post" id="mG61Hd"> contains the post URL
post_url = html.find('form', attrs={'id': 'mG61Hd'})
print(post_url['action'])

# Use the post method of the requests module to POST your data:
r = requests.post(post_url['action'], data = {'key':'value'})
print(r)
Related