[英]Simulate a “Click” with Python
I have read in all topics but is not so clear how to simulate a click in python. 我已经阅读了所有主题,但不清楚如何模拟python中的点击。 I'm using requests
but I didn't understand how to simulate it. 我正在使用requests
但是我不知道如何模拟requests
。
The code that I need to "click" is that: 我需要“单击”的代码是:
<div class="container"> <a class="brand" href=url> <img src="logo2.png" alt="Logo"></a>
<div class="page-container">
<form class='addf' action="" method="post">
<h1>url server</h1>
<p>
Welcome to url server!
</p>
<input type="text" name="Username" id="Username" readonly placeholder="Click to generate your username...">
<input type="hidden" name="formid" value="32bbba790d2a75a5dafec2ec6c3bbc19" />
<button name='urlline' type="submit">Generate now!</button>
</form>
</div>
Thanks to everyone in advance 预先感谢大家
If you know which action the form posts to, you may be able to do it by posting directly combined with Beautiful Soup . 如果您知道表单要执行的操作,则可以直接与Beautiful Soup一起进行发布。
The line: <input type="hidden" name="formid" value="32bbba790d2a75a5dafec2ec6c3bbc19" />
is important, as this hash was most likely generated when the page was served. 该行: <input type="hidden" name="formid" value="32bbba790d2a75a5dafec2ec6c3bbc19" />
很重要,因为该哈希很可能是在提供页面时生成的。 This is done to combat DDoS, eg someone spamming requests to the form-action. 这样做是为了对抗DDoS,例如有人向表单操作发送了垃圾邮件。 So in order for the webserver to accept your request, you will have to inspect this value and and pass it along your POST request. 因此,为了使Web服务器接受您的请求,您将必须检查该值并将其传递给POST请求。
You could do something like this: 您可以执行以下操作:
import requests
from bs4 import BeautifulSoup
url = "http://some-url/" # replace with target URL
r = requests.get(url)
if r.status_code == 200:
bs = BeautifulSoup(r.text)
form = bs.findAll("form", {"class": "addf"})[0] # find the form
inputs = form.findAll("input") # find the input-fields
hash = None
for input in inputs:
if input.get("name") == "formid": # find the hash
hash = input.get("value")
if hash:
action = "createusername" # replace with target action
res = requests.post(url + action, data={
# ... other parameters, if any
"formid" : hash
})
print(res)
You may need to refine how Beautiful Soup searches the HTML, eg if multiple elements have class="addf"
. 您可能需要优化Beautiful Soup如何搜索HTML,例如,如果多个元素具有class="addf"
。
您可以使用chrome的开发人员工具观察网络流量,然后使用请求库模拟http请求。
this is working for me: 这为我工作:
import requests as req
import random
import math
username = "";
payload = {'Username': username,'password': 'password'}
resp = req.post(url, data=payload)
thank you! 谢谢! :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.