12

次のような Google フォームで応答を送信できるようにする Python スクリプトを作成しようとしています: https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/viewform

しかし、実際に POST を送信する方法と、この POST に実際に何が含まれているかを確認するにはどうすればよいですか?

4

4 に答える 4

24

初めpip install requests

特定のフォーム データを特定の URL に投稿する必要があります。リクエストを使用できます。form_data dict パラメータはオプションに対応しています。オプションが必要ない場合は、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)
于 2013-07-31T08:39:44.130 に答える
6

@pigletfly からの回答に基づいて、フィールド名を収集するための小さなスクリプトを作成しました (テキスト フィールドのみのフォームの場合)。

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)

次に、get_questions 関数を使用して、入力できるフィールドを取得できます

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

anno_questions = get_questions(TEST_FORM_URL)

質問 (フィールド) を dict として取得するには

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

次に、submit_response とキーワード引数を使用して送信します

submit_response(TEST_FORM_URL, anno_questions, annotator="TestUser", item_id = 0)
于 2018-08-03T15:03:03.753 に答える
1

と を使用urllib2urllibて投稿を送信します。

次のようにします。

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

そのように少し構造化する必要があります。

フォーム名を取得するには、サイトのソース コードを調べて、入力して送信するフォームの名前を見つける必要があります。

お役に立てれば

幸運を:)

于 2013-07-31T07:49:04.063 に答える
1

動作するスクリプトは次のとおりです。

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)
于 2014-01-13T03:04:14.317 に答える