我已阅读所有主题,但不太清楚如何模拟python中的单击。我正在使用requests
,但我不明白如何模拟它。
我需要“点击”的代码是:
<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>
感谢大家提前
如果您知道表单发布的操作,您可以通过直接与Beautiful Soup结合发布。
行:<input type="hidden" name="formid" value="32bbba790d2a75a5dafec2ec6c3bbc19" />
很重要,因为这个哈希很可能是在提供页面时生成的。这样做是为了打击DDoS,例如有人向表单操作发送垃圾邮件请求。因此,为了让Web服务器接受您的请求,您必须检查此值并将其传递给您的POST请求。
你可以这样做:
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)
您可能需要优化Beautiful Soup如何搜索HTML,例如,如果多个元素具有class="addf"
。
您可以使用chrome'开发人员工具来观察网络流量,然后使用请求库来模拟http请求。
这对我有用:
import requests as req
import random
import math
username = "";
payload = {'Username': username,'password': 'password'}
resp = req.post(url, data=payload)
谢谢! :)