本文目录导读:

我来介绍几种常见的登录模拟脚本写法,主要使用Python语言。
使用 Selenium(模拟浏览器)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# 初始化浏览器
driver = webdriver.Chrome() # 确保已安装chromedriver
try:
# 访问登录页面
driver.get("https://example.com/login")
# 等待页面加载
wait = WebDriverWait(driver, 10)
# 定位并填写用户名和密码
username_input = wait.until(EC.presence_of_element_located((By.ID, "username")))
username_input.send_keys("your_username")
password_input = driver.find_element(By.ID, "password")
password_input.send_keys("your_password")
# 点击登录按钮
login_button = driver.find_element(By.ID, "login-btn")
login_button.click()
# 等待登录完成
time.sleep(3)
# 检查是否登录成功(检测元素是否存在)
try:
success_element = driver.find_element(By.CLASS_NAME, "user-info")
print("登录成功!")
except:
print("登录失败")
finally:
driver.quit() # 关闭浏览器
使用 requests 发送 POST 请求
import requests
# 登录信息
login_data = {
"username": "your_username",
"password": "your_password",
"csrf_token": "" # 如果有CSRF token需要获取
}
# 创建会话保持cookies
session = requests.Session()
# 1. 首先获取登录页面(获取必要的token)
login_page = session.get("https://example.com/login")
# 解析CSRF token(如果有)
# 示例:从HTML中提取token
from bs4 import BeautifulSoup
soup = BeautifulSoup(login_page.text, 'html.parser')
csrf_token = soup.find('input', {'name': 'csrf_token'})['value']
login_data['csrf_token'] = csrf_token
# 2. 发送登录请求
login_url = "https://example.com/api/login"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Content-Type": "application/x-www-form-urlencoded"
}
response = session.post(login_url, data=login_data, headers=headers)
# 3. 检查登录结果
if response.status_code == 200:
print("登录成功!")
# 后续请求可以直接使用session
dashboard = session.get("https://example.com/dashboard")
print(dashboard.text)
else:
print(f"登录失败,状态码:{response.status_code}")
处理验证码
from selenium import webdriver
import pytesseract
from PIL import Image
import time
def recognize_captcha(driver, captcha_element):
"""识别验证码(简单示例,实际可能需要更复杂的处理)"""
# 截图验证码区域
location = captcha_element.location
size = captcha_element.size
# 保存验证码图片
driver.save_screenshot("screenshot.png")
image = Image.open("screenshot.png")
captcha_image = image.crop((
location['x'],
location['y'],
location['x'] + size['width'],
location['y'] + size['height']
))
captcha_image.save("captcha.png")
# OCR识别
text = pytesseract.image_to_string(captcha_image, lang='eng')
return text.strip()
# 使用示例
driver = webdriver.Chrome()
driver.get("https://example.com/login")
# 获取验证码元素
captcha_element = driver.find_element(By.ID, "captcha-image")
captcha_code = recognize_captcha(driver, captcha_element)
# 输入验证码
captcha_input = driver.find_element(By.ID, "captcha")
captcha_input.send_keys(captcha_code)
完整的登录脚本模板
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
import time
class LoginSimulator:
def __init__(self, use_browser=False):
self.use_browser = use_browser
if use_browser:
self.driver = webdriver.Chrome()
else:
self.session = requests.Session()
def login_with_browser(self, url, username, password):
"""使用浏览器登录"""
driver = self.driver
try:
driver.get(url)
wait = WebDriverWait(driver, 10)
# 填写登录表单
username_input = wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='username']"))
)
username_input.send_keys(username)
password_input = driver.find_element(By.CSS_SELECTOR, "input[name='password']")
password_input.send_keys(password)
# 点击登录
login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
login_button.click()
time.sleep(3) # 等待页面跳转
# 保存cookies
cookies = driver.get_cookies()
cookies_dict = {c['name']: c['value'] for c in cookies}
return cookies_dict
except Exception as e:
print(f"登录失败: {e}")
return None
def login_with_requests(self, url, username, password):
"""使用requests登录"""
try:
# 获取登录页面获取token
response = self.session.get(url)
# 解析需要的token
# 这里需要根据实际情况修改
csrf_token = "" # 从页面中提取
login_data = {
"username": username,
"password": password,
"csrf_token": csrf_token,
"remember_me": True
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Referer": url
}
login_response = self.session.post(
url + "/login",
data=login_data,
headers=headers
)
if login_response.status_code == 200:
print("登录成功")
return self.session.cookies.get_dict()
else:
print(f"登录失败: {login_response.status_code}")
return None
except Exception as e:
print(f"登录异常: {e}")
return None
def __del__(self):
if self.use_browser and hasattr(self, 'driver'):
self.driver.quit()
# 使用示例
if __name__ == "__main__":
# 使用浏览器模拟
simulator = LoginSimulator(use_browser=True)
cookies = simulator.login_with_browser(
"https://example.com/login",
"username",
"password"
)
# 使用requests
# simulator = LoginSimulator(use_browser=False)
# cookies = simulator.login_with_requests(
# "https://example.com",
# "username",
# "password"
# )
注意事项
- 遵守法律:只对自己有权限的系统进行测试
- 反爬机制:注意网站的反爬策略,适当添加延时
- 凭证安全:不要在代码中硬编码密码,使用环境变量
- 验证码处理:简单的OCR可能不够,可能需要使用专业服务
- 动态网站:部分网站使用Ajax,需要分析实际网络请求
选择哪种方式取决于目标网站的具体实现和你的需求。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播