-->

自动按使用python“提交”按钮(Automatically pressing a “submit

2019-07-29 16:15发布

我用的是公交公司运行一个可怕的网站( 希伯来语 , 英语 ),它制作一个简单的“从A到B的时间表今天”查询一场噩梦。 我怀疑他们正试图鼓励昂贵的短信查询系统的使用。

我试图从网站,提交对每一个可能的点查询到每一个可能的点,这将之和为约10K查询到收获的整个时间表。 查询结果将显示在一个弹出式窗口。 我是很新的网络编程,但熟悉Python的基本方面。

  1. 什么是解析页面最优雅的方式,选择来回下拉菜单中选择一个值,然后按“提交”使用脚本?
  2. 如何给该程序的新弹出的输入的内容是什么?

谢谢!

Answer 1:

斜纹是Web浏览一个简单的脚本语言。 它发生在体育运动中使用Python API中 。

斜纹基本上围绕机械化包装的薄壳。 所有斜纹命令在commands.py文件中实现,并pyparsing做解析输入,并将其转换成Python命令(见parse.py)的工作。 交互式shell工作和readline支持经由CMD模块(从标准Python库)来实现。

“按压”的一个例子从上述链接文档提交:

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')


Answer 2:

我会建议你使用机械化 。 下面是从他们的网页的代码片段展示了如何提交一个表单:


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



Answer 3:

你很少想实际“按提交按钮”,而不是让GET或POST请求直接处理程序资源。 看看那里的形式是HTML,看看它的参数提交到哪个网址,如果它是GET或POST方法。 可以形成的urllib(2)很轻松了这些请求。



文章来源: Automatically pressing a “submit” button using python