Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google-Forms response with Python?

I'm trying to write a Python-Script which makes it possible to submit responses in Google-Forms like this one: https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/viewform

But how to I actually send the POST and how can I find out, what this POST should actually contain?

like image 939
Exceen Avatar asked Jul 31 '13 07:07

Exceen


2 Answers

First pip install requests

You have to post some specific form data to a specific url,you can use requests.The form_data dict params are correspondent to options,if you don't need some options,just remove it from form_data.

import requests
url = 'https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/formResponse'
form_data = {'entry.2020959411':'18+ sollte absolute Pflicht sein',
            'entry.2020959411':'Alter sollte garkeine Rolle spielen',
            'entry.2020959411':'17+ wäre für mich vertretbar',
            'entry.2020959411':'16+ wäre für mich vertretbar',
            'entry.2020959411':'15+ wäre für mich vertretbar',
            'entry.2020959411':'Ausnahmen von der Regel - Dafür?',
            'entry.2020959411':'Ausnahmen von der Regel - Dagegen?',
            'entry.2020959411':'__other_option__',
            'entry.2020959411.other_option_response':'test',
            'draftResponse':[],
            'pageHistory':0}
user_agent = {'Referer':'https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/viewform','User-Agent': "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36"}
r = requests.post(url, data=form_data, headers=user_agent)
like image 126
pigletfly Avatar answered Oct 01 '22 02:10

pigletfly


Based on the answer from @pigletfly I wrote a little script for harvesting the field names (for a text-field only form)

import urllib.request
from bs4 import BeautifulSoup
import requests, warnings
def get_questions(in_url):
    res = urllib.request.urlopen(in_url)
    soup = BeautifulSoup(res.read(), 'html.parser')
    get_names = lambda f: [v for k,v in f.attrs.items() if 'label' in k]
    get_name = lambda f: get_names(f)[0] if len(get_names(f))>0 else 'unknown'
    all_questions = soup.form.findChildren(attrs={'name': lambda x: x and x.startswith('entry.')})
    return {get_name(q): q['name'] for q in all_questions}
def submit_response(form_url, cur_questions, verbose=False, **answers):
    submit_url = form_url.replace('/viewform', '/formResponse')
    form_data = {'draftResponse':[],
                'pageHistory':0}
    for v in cur_questions.values():
        form_data[v] = ''
    for k, v in answers.items():
        if k in cur_questions:
            form_data[cur_questions[k]] = v
        else:
            warnings.warn('Unknown Question: {}'.format(k), RuntimeWarning)
    if verbose:
        print(form_data)
    user_agent = {'Referer':form_url,
                  'User-Agent': "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36"}
    return requests.post(submit_url, data=form_data, headers=user_agent)

You can then use the get_questions function to get the fields which you can fill in

TEST_FORM_URL = "https://docs.google.com/forms/d/e/1FAIpQLSfBmvqCVeDA7IZP2_mw_HZ0OTgDk2a0JN4VlY5KScECWC-_yw/viewform"

anno_questions = get_questions(TEST_FORM_URL)

To get the questions (fields) as a dict

{'annotator': 'entry.756364489',
 'task': 'entry.1368373366',
 'item_id': 'entry.84713541',
 'label': 'entry.2072511216',
 'session': 'entry.2021127767',
 'time': 'entry.1122475936'}

then use the submit_response with keyword arguments to submit

submit_response(TEST_FORM_URL, anno_questions, annotator="TestUser", item_id = 0)
like image 41
kmader Avatar answered Oct 01 '22 03:10

kmader