python 使用python自动按下“提交”按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1082361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:26:17  来源:igfitidea点击:

Automatically pressing a "submit" button using python

pythonscriptingform-submitdata-harvest

提问by Adam Matan

The bus company I use runs an awful website (Hebrew,English) which making a simple "From A to B timetable today" query a nightmare. I suspect they are trying to encourage the usage of the costly SMS query system.

我使用的巴士公司经营着一个糟糕的网站(希伯来语英语),这使得简单的“今天从 A 到 B 的时间表”查询成为一场噩梦。我怀疑他们试图鼓励使用昂贵的 SMS 查询系统。

I'm trying to harvest the entire timetable from the site, by submitting the query for every possible point to every possible point, which would sum to about 10k queries. The query result appears in a popup window. I'm quite new to web programming, but familiar with the basic aspects of python.

我正在尝试通过将每个可能点的查询提交到每个可能的点来从站点获取整个时间表,这将总计约 10k 次查询。查询结果出现在弹出窗口中。我对网络编程很陌生,但熟悉 python 的基本方面。

  1. What's the most elegant way to parse the page, select a value fro a drop-down menu, and press "submit" using a script?
  2. How do I give the program the contents of the new pop-up as input?
  1. 解析页面、从下拉菜单中选择一个值并使用脚本按“提交”的最优雅方法是什么?
  2. 如何将新弹出窗口的内容作为输入提供给程序?

Thanks!

谢谢!

回答by gimel

Twillis a simple scripting language for Web browsing. It happens to sport a python api.

Twill是一种用于 Web 浏览的简单脚本语言。它碰巧有一个python api

twill is essentially a thin shell around the mechanize package. All twill commands are implemented in the commands.py file, and pyparsing does the work of parsing the input and converting it into Python commands (see parse.py). Interactive shell work and readline support is implemented via the cmd module (from the standard Python library).

斜纹布本质上是围绕机械化包装的薄壳。所有 twill 命令都在 commands.py 文件中实现,pyparsing 负责解析输入并将其转换为 Python 命令(参见 parse.py)。交互式 shell 工作和 readline 支持是通过 cmd 模块(来自标准 Python 库)实现的。

An example of "pressing" submit from the above linked doc:

从上面链接的文档中“按下”提交的示例:

from twill.commands import go, showforms, formclear, fv, submit

go('http://issola.caltech.edu/~t/qwsgi/qwsgi-demo.cgi/')
go('./widgets')
showforms()

formclear('1')
fv("1", "name", "test")
fv("1", "password", "testpass")
fv("1", "confirm", "yes")
showforms()

submit('0')

回答by Geo

I would suggest you use mechanize. Here's a code snippet from their page that shows how to submit a form :

我建议你使用mechanize。这是他们页面中的代码片段,显示了如何提交表单:


import re
from mechanize import Browser

br = Browser()
br.open("http://www.example.com/")
# follow second link with element text matching regular expression
response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1)
assert br.viewing_html()
print br.title()
print response1.geturl()
print response1.info()  # headers
print response1.read()  # body
response1.close()  # (shown for clarity; in fact Browser does this for you)

br.select_form(name="order")
# Browser passes through unknown attributes (including methods)
# to the selected HTMLForm (from ClientForm).
br["cheeses"] = ["mozzarella", "caerphilly"]  # (the method here is __setitem__)
response2 = br.submit()  # submit current form

# print currently selected form (don't call .submit() on this, use br.submit())
print br.form

回答by ironfroggy

You very rarely want to actually "press the submit button", rather than making GET or POST requests to the handler resource directly. Look at the HTML where the form is, and see what parameters its submitting to what URL, and if it is GET or POST method. You can form these requests with urllib(2) easily enough.

您很少想真正“按下提交按钮”,而不是直接向处理程序资源发出 GET 或 POST 请求。查看表单所在的 HTML,看看它提交给什么 URL 的参数,以及它是 GET 还是 POST 方法。您可以很容易地使用 urllib(2) 形成这些请求。