【问题标题】:How to go to next page on google form using requests.post如何使用 requests.post 转到谷歌表单的下一页
【发布时间】:2020-08-31 12:59:01
【问题描述】:

我查看了多个关于如何填写谷歌表单的教程,并成功地完成了它。我的问题是 google 表单在您提交之前有 2 页。

我已经创建了我的表单数据:

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

发帖

    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)

将页面历史记录更改为 1 会填写第二页的数据,将其设置为 0 会填写第一页的数据。我尝试使用 2 个具有不同页面历史记录的 requests.post,但它只是创建了 2 个单独的谷歌表单响应。还有更多表单数据,但我没有包括它。所有 entry.id 都是正确的。

【问题讨论】:

  • 你好,你能看看你的代码吗?标题 form_data 和 post 方法之间似乎有些混乱。
  • @pyOliv 我修复了格式错误
  • 好吧,抱歉,代码不可执行,而且我们没有 URL。可以分享一下吗?
  • @pyOliv url 在用户代理中,你是什么意思它不可执行?
  • 当我搜索 url 时,我找到了gsuite.google.com。它显然不是您使用的那个。另外,如果我复制粘贴上面的代码,我会出错......你能分享一段有效的代码吗?

标签: python python-3.x python-requests python-requests-html


【解决方案1】:

试试这个代码。我已经评论它以了解它是如何工作的。

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)

【讨论】:

  • 我认为这行得通。它正在发送空响应,但仍然无法在表单的下一页上添加响应,这是我的问题。我能够在第一页上发送响应,但脚本无法到达第二页上的最后一个响应。
猜你喜欢
  • 2020-05-31
  • 2021-12-04
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
相关资源
最近更新 更多