下载地址:https://www.pan38.com/share.php?code=pvvmX 提取码:8888
自动化脚本,用于检测订单中的距离和价格,并在符合条件时自动执行点击操作。这个脚本将使用Python和Selenium实现,包含异常处理和日志记录功能。
这个脚本实现了完整的订单自动检测和点击功能。主要特点包括:1) 使用Selenium进行网页自动化操作 2) 完善的日志记录系统 3) 异常处理和重试机制 4) 可配置的距离和价格阈值。使用时需要安装selenium库和对应浏览器驱动。
import time
import logging
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
from selenium.common.exceptions import TimeoutException, NoSuchElementException
class OrderAutoClicker:
def init(self):
# 初始化日志记录
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='order_clicker.log'
)
self.logger = logging.getLogger(__name__)
# 配置浏览器选项
options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
options.add_argument('--start-maximized')
try:
self.driver = webdriver.Chrome(options=options)
self.wait = WebDriverWait(self.driver, 15)
self.logger.info("浏览器初始化成功")
except Exception as e:
self.logger.error(f"浏览器初始化失败: {str(e)}")
raise
def check_conditions(self, distance_threshold, price_threshold):
"""检查订单是否符合条件"""
try:
# 获取距离信息
distance_element = self.wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".distance"))
)
distance_text = distance_element.text
distance = float(distance_text.replace('km', '').strip())
# 获取价格信息
price_element = self.wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".price"))
)
price_text = price_element.text
price = float(price_text.replace('¥', '').strip())
self.logger.info(f"检测到订单: 距离 {distance}km, 价格 ¥{price}")
# 检查是否符合条件
if distance <= distance_threshold and price >= price_threshold:
self.logger.info("订单符合条件")
return True
return False
except (TimeoutException, NoSuchElementException, ValueError) as e:
self.logger.warning(f"获取订单信息时出错: {str(e)}")
return False
def click_order(self):
"""点击符合条件的订单"""
try:
accept_button = self.wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".accept-btn"))
)
accept_button.click()
self.logger.info("成功点击订单")
return True
except Exception as e:
self.logger.error(f"点击订单时出错: {str(e)}")
return False
def process_orders(self, url, distance_threshold=5.0, price_threshold=30.0):
"""处理订单流程"""
try:
self.driver.get(url)
self.logger.info(f"已访问URL: {url}")
while True:
try:
# 等待订单加载
self.wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".order-item"))
)
# 检查订单条件
if self.check_conditions(distance_threshold, price_threshold):
if self.click_order():
# 点击成功后等待一段时间
time.sleep(5)
break
# 如果没有符合条件的订单,刷新页面
self.driver.refresh()
self.logger.info("刷新页面等待新订单...")
time.sleep(3)
except KeyboardInterrupt:
self.logger.info("用户中断操作")
break
except Exception as e:
self.logger.error(f"处理订单时出错: {str(e)}")
time.sleep(5)
finally:
self.driver.quit()
self.logger.info("浏览器已关闭")
if name == "main":
try:
# 配置参数
TARGET_URL = "https://example.com/orders" # 替换为实际订单页面URL
MAX_DISTANCE = 3.5 # 最大可接受距离(km)
MIN_PRICE = 25.0 # 最小可接受价格(元)
clicker = OrderAutoClicker()
clicker.process_orders(TARGET_URL, MAX_DISTANCE, MIN_PRICE)
except Exception as e:
print(f"程序运行出错: {str(e)}")