Python 带有请求的“单击”按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37164675/
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
"Clicking" button with requests
提问by Repcak
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).
我有这个小网站,我想用请求库填写表格。问题是我在填写表单数据并点击按钮时无法进入下一个站点(Enter 不起作用)。
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.
前三个条目 name、message、sign 是文本区域,而 step 是我认为的按钮。
r = requests.get(url)
r = requests.post(url, data=info)
print(r.text)
The Form Data looks like this when i send a request via chrome manually:
当我通过 chrome 手动发送请求时,表单数据看起来像这样:
- name:JohnJohn
- message:XXX
- sign:XXX
- step:1
- 姓名:约翰约翰
- 留言:XXX
- 签名:XXX
- 第1步
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.
如果我手动执行此操作,下一个站点具有相同的地址。
回答by Dave
As you might see from the snipped you posted, clicking the button is triggering some JavaScript code, namely a method called myClick()
.
正如您从发布的片段中看到的那样,单击按钮会触发一些 JavaScript 代码,即一个名为myClick()
.
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.
使用 python 请求库点击这个东西并不简单。您可能会更幸运地尝试找出内部发生的事情myClick()
。我的猜测是,在某个时候,POST
将向 HTTP 端点发出请求。如果你能弄明白这一点,你就可以把它翻译成你的python代码。
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.
如果这不起作用,另一种选择是使用 Selenium/PhantomJS 之类的东西,它使您能够拥有一个真正的、无头的和可编写脚本的浏览器。使用这样的工具,您实际上可以让它填写表格并单击按钮。你可以看看这个so answer,因为它向你展示了如何从 python 使用 Selenium+PhantomJS。
Please make sure not to abuse such methods by spamming forums or [insert illegal or otherwise abusive activity here].
请确保不要通过向论坛发送垃圾邮件或 [在此处插入非法或其他滥用活动] 来滥用此类方法。
回答by fnr
In such a situation when you need to forge scripted button's request, it may be easier not to guess the logic of JS but instead perform a physical click and look into chrome devtools' network sniffer which gives you a plain request made which, in turn, can be easily forged in Python
在这种情况下,当您需要伪造脚本按钮的请求时,不猜测 JS 的逻辑可能会更容易,而是执行物理单击并查看 chrome devtools 的网络嗅探器,它为您提供了一个简单的请求,反过来,可以很容易地在 Python 中伪造