首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自动填写或自动化Google表单

自动填写或自动化Google表单
EN

Stack Overflow用户
提问于 2020-05-17 09:36:38
回答 6查看 44.8K关注 0票数 16

最近,预算学校糟糕地实施了家庭式学习,我现在正面临着一个愚蠢的机器人般的任务,每天为我的孩子每一个孩子安排几次出勤率。学校共用以下表格:

作为一名开发人员,我希望创建一个UI,并让我的孩子在老师要求的时候自己提交这个表格的出勤率。有可能吗?我是一个.Net开发人员,没有为Office 365在线开发任何东西,也不知道从哪里开始寻找谷歌。

EN

Stack Overflow用户

发布于 2020-12-02 20:51:08

我想我可能迟到了,但还是给了你一个解决办法。我也做了类似的剧本来送我的学生上学。Google中的每个字段都与entry.<id>相关联。您有两种方法可以使表单自动化。

一种方法是使用entry.<id>提取这些ID,并制作一个字典,其中的键是value.Then,您的回答是value.Then,您必须向表单URL发送一个POST请求,并将字典作为数据。你已经把表格自动化了。

要提取ID,请检查html代码并查看页面(非常)末端的<script>。就像这样:

代码语言:javascript
运行
复制
var FB_PUBLIC_LOAD_DATA_ = [null,[null,[[2030831236,"First Name (in English)",null,0,[[1475351979,null,1]
]
]
,[86681139,"Last Name (in English)",null,0,[[280503419,null,1]
]
]
,[836880978,"Grade",null,2,[[519373518,[["KG 1",null,null,null,0]
,["KG 2",null,null,null,0]
,["1",null,null,null,0]
,["2",null,null,null,0]
,["3",null,null,null,0]
,["4",null,null,null,0]
,["5",null,null,null,0]
,["6",null,null,null,0]
,["7",null,null,null,0]
,["8",null,null,null,0]
,["9",null,null,null,0]
,["10",null,null,null,0]
,["11",null,null,null,0]
,["12",null,null,null,0]
]
,1,null,null,null,null,null,0]
]
]
,[221348070,"Section",null,2,[[301819105,[["A",null,null,null,0]
,["B",null,null,null,0]
,["C",null,null,null,0]
,["D",null,null,null,0]
,["E",null,null,null,0]
,["G",null,null,null,0]
]
,1,null,null,null,null,null,0]
]
]
,[366027193,"Subject",null,2,[[1124370742,[["Math",null,null,null,0]
,["Science",null,null,null,0]
,["English",null,null,null,0]
,["Arabic",null,null,null,0]
,["Islamic",null,null,null,0]
,["Social",null,null,null,0]
,["Moral",null,null,null,0]
,["Art",null,null,null,0]
,["Computer",null,null,null,0]
,["French",null,null,null,0]
,["Physics",null,null,null,0]
,["Chemistry",null,null,null,0]
,["Biology",null,null,null,0]
,["Business",null,null,null,0]
]
,1,null,null,null,null,null,0]
]
]
]
,null,null,[null,null,null,null,null,[null,null,null,[3,169,244,null,1]
,[217,242,253,null,1]
]
]
,null,null,null,"Attendance Form",48,null,null,null,null,null,[2]
]

如您所见,每个字段有两个数字。其中一个是身份证,另一个我不知道。第二个号码是我们需要的ID。使用RegEx,我们可以提取所有的数字并收集列表中的每一个秒数。此列表将包含所有ID。

另一种方法,正如其他人所说的,是用预先填充的值重构URL。但是在这个问题上,你也必须提取ID。

我把这两个人都写进了一个剧本:

代码语言:javascript
运行
复制
import requests
from bs4 import BeautifulSoup
import re

def get_questions(url):
    
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    
    content = soup.body.find_all(text = re.compile('var FB'))
    
    match = re.findall('[,]["][\w\s]+["][,]', str(content))
    #It will match all the questions in the form
    question_strings = [x.strip('"') for x in match]
    
    match_ids = re.findall('(?<=\[\[)(\d+)', str(content))
    #It will find all the numbers in the content
    question_ids = ['entry.' + x for x in match_ids[1:]]
    #It will leave the first numbers (they are not the ids)
    return question_ids
    
# Below are only for when you want to know the form fills with their corresponding entry ids
#    questions = dict(zip(question_strings, question_ids))    
#    return questions


def send_answers(url, fname, lname, grade, section, subject): #arrange this as per your form requirements
    
    ids = get_questions(url)
    
    answers = [fname, lname, grade, section, subject]
    response = dict(zip(ids, answers))
    
    if 'viewform' in url:
        s = url.index('viewform') 
        response_url = url.replace(url[s::], 'formResponse?')
        
    try:
        r = requests.post(response_url, response)
        if r.status_code == 200:
            return '[!] Attendence posted !'
        #In case an error happens, it will raise an exception
        else:
            raise Exception

    #After raising the exception it will retry to submit using url reconstruction with prefilled values
    except:
        try:
            ans_list = [x + '=' + y for x, y in zip(ids, answers)]
            
            for i in range(0, len(ans_list)):
                response_url += ans_list[i]
                response_url += '&'
                
            response_url.strip("&")    
            r = requests.get(response_url)
            status = r.status_code
            
            if status == 200:
                return '[!] Attendance sent !'
            else:
                raise Exception
        #If still an error happens, it will print out a message.
        except:
            return '[!] Attendance not sent !'
                

url = 'Form URL here'

fname = 'Your first name here'
lname = 'Your last name here'
grade = 'Your grade here'
section = 'Section here'
subject = 'Enter subject'

print(send_answers(url, fname, lname, grade, section, subject))

希望能帮上忙。对不起我的英语不好。

票数 5
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61849716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档