“点击"带有请求的按钮 [英] "Clicking" button with requests

查看:121
本文介绍了“点击"带有请求的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小网站,我想用请求库填写表格.问题是填写表单数据并单击按钮时,我无法到达下一个站点(输入不起作用).

I have this little website i want to fill in a form with the requests library. The problem is i cant get to the next site when filling the form data and hitting the button(Enter does not work).

重要的是,我无法通过某种类型的点击bot来做到这一点.这需要完成,这样我才能在没有图形的情况下运行.

The important thing is I can't do it via a clicking bot of some kind. This needs to be done so I can run in without graphics.

info = {'name':'JohnJohn',
        'message':'XXX',
        'sign':"XXX",
        'step':'1'}

前三个条目的名称,消息,符号是文本区域,步骤是我认为的按钮.

First three entries name, message, sign are the text areas and step is I think the button.

r = requests.get(url)
r = requests.post(url, data=info)

print(r.text)

当我通过chrome手动发送请求时,表单数据如下所示:

The Form Data looks like this when i send a request via chrome manually:

  • 名称:JohnJohn
  • 消息:XXX
  • 签名:XXX
  • 步骤:1

button元素如下所示:

The button element looks like this:

<td colspan="2" style="text-align: center;">
    <input name="step" type="hidden" value="1">
    <button id="button" type="button" onclick="myClick();"
     style="background-color: #ef4023;  width: 80px; font-face: times; font-size: 14pt;">
        Wyślij
    </button>
</td>

下一个站点(如果我手动执行此操作)具有相同的地址.

The next site if i do this manually has the same adres.

推荐答案

正如您从发布的摘要中看到的那样,单击按钮会触发一些JavaScript代码,即称为myClick()的方法.

As you might see from the snipped you posted, clicking the button is triggering some JavaScript code, namely a method called myClick().

使用pythons请求库单击该对象并非易事.您可能有更多的运气试图找出myClick()内部发生的情况.我的猜测是,在某个时候,将对HTTP端点发出POST请求.如果可以弄清楚,可以将其转换为python代码.

It is not straightforward to click on this thing using pythons requests library. You might have more luck trying to find out what happens inside myClick(). My guess would be that at some point, a POST request will be made to a HTTP endpoint. If you can figure this out, you can translate it into your python code.

如果这不起作用,另一种选择是使用Selenium/PhantomJS之类的东西,它使您能够拥有一个真正的,无头且可编写脚本的浏览器.使用这样的工具,您实际上可以填写表格并单击按钮.您可以看一下答案,因为它向您展示了如何从python使用Selenium + PhantomJS.

If that does not work, another option would be to use something like Selenium/PhantomJS, which give you the ability to have a real, headless and scriptable browser. Using such a tool, you can actually have it fill out forms and click buttons. You can have a look at this so answer, as it shows you how to use Selenium+PhantomJS from python.

请确保不要通过向论坛发送垃圾邮件或[在此处插入非法或其他侮辱性活动]来滥用此类方法.

Please make sure not to abuse such methods by spamming forums or [insert illegal or otherwise abusive activity here].

这篇关于“点击"带有请求的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆