Google Forms answer with Python?

I am trying to write a Python-Script that allows you to submit answers in Google Forms, like this one: https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/viewform

But how do I really send POST and how can I find out what this POST should contain?

+6
source share
3 answers

pip install requests

You must send some data of a specific form to a specific URL, you can use requests. The form_data dict parameters match the parameters, if you do not need some parameters, just remove them 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) 
+12
source

I would use urllib2 and urllib to send a message.

Do something like this:

 import urllib2, urllib import cookielib cookieJar = cookielib.LWPCookieJar() opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(self.cookieJar), # Create Opener urllib2.HTTPRedirectHandler(), urllib2.HTTPHandler(debuglevel=0)) # Add Headers opener.addheaders = [('User-agent', "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36")] forms = { "formname": value, # The forms name and the selected value you want "formname2": value2, } data = urllib.urlencode(forms) # Encode data req = urllib2.Request('http://www.example.com',data) # Send Request res = opener.open(req) # Open Request html = res.read() # Read Response 

you have to structure it a little.

To get the names of the forms, you need to look at the source code of the site and find the names of the forms that you want to enter and submit.

Hope this helps

Good luck :)

0
source

Here is my script that works:

 import urllib import urllib2 user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)' header={'User-Agent' : user_agent} url = "http://....Your google form" # values from your form. You will need to include any hidden variables if you want to.. values= { 'entry.asdfsdfsdasd': 'asdfasdfsd', 'draftResponse':'[,,"-asdfasdasdf"]', 'pageHistory':'0', 'fbzx':'-asdfasdfsd' } data = urllib.urlencode(values) urllib2.Request(url, data, header) 
0
source

Source: https://habr.com/ru/post/950692/


All Articles