我写了一个关于模拟计算彩票的模型,但系统一直去报错Edge浏览器未能找到匹配XPath...

查看 43|回复 1
作者:maoyan   
源代码
[Python] 纯文本查看 复制代码import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
import requests
from bs4 import BeautifulSoup
import selenium
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
# 创建Edge WebDriver实例
driver_path = "path_to_edge_webdriver"  # 将"path_to_edge_webdriver"替换为你的Edge WebDriver可执行文件路径
service = Service(driver_path)
driver = webdriver.Edge(service=service)
# 使用WebDriver加载网页
url = "https://datachart.500.com/ssq/history/history.shtml"
driver.get(url)
# 等待页面加载完成(可选)
# 等待5秒钟
import time
time.sleep(5)
# 获取表格元素
table = driver.find_element(By.XPATH, "//table[contains(@class, 'trend-table')]")
# 提取表格数据
rows = table.find_elements_by_tag_name("tr")
header_row = rows[1]
data_rows = rows[2:]
# 解析表头
header_cells = header_row.find_elements_by_tag_name("th")
header = [cell.text.strip() for cell in header_cells]
# 解析数据行
data = []
for row in data_rows:
    cells = row.find_elements_by_tag_name("td")
    row_data = [cell.text.strip() for cell in cells]
    data.append(row_data)
# 将WebDriver关闭
driver.quit()
# 将数据转换为DataFrame
data = pd.DataFrame(data, columns=header)
# 将红球列和蓝球列转换为字符串类型
data["红球"] = data["红球"].astype(str)
data["蓝球"] = data["蓝球"].astype(str)
# 获取过去100期的开奖号码
past_draws = data[-100:]
# 特征工程
# 将红球历史开奖号码拆分成6个独立的特征
red_balls = past_draws["红球"].apply(lambda x: pd.Series(x.split(' ')))
red_balls.columns = [f"红球{i+1}" for i in range(6)]
# 将蓝球历史开奖号码作为目标
target = past_draws["蓝球"]
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(red_balls, target, test_size=0.2, random_state=42)
# 特征编码
encoder = OneHotEncoder(categories="auto", sparse=False, handle_unknown="ignore")
X_train_encoded = encoder.fit_transform(X_train)
X_test_encoded = encoder.transform(X_test)
# 构建随机森林回归模型
model = RandomForestRegressor(random_state=42)
# 模型训练
model.fit(X_train_encoded, y_train)
# 模型评估
y_pred = model.predict(X_test_encoded)
mse = mean_squared_error(y_test, y_pred)
print("过去100期的均方误差(MSE):", mse)
# 预测下一期的开奖号码
last_draw = red_balls.iloc[-1].values.reshape(1, -1)
next_draw = model.predict(encoder.transform(last_draw))
print("下一期的开奖号码预测:红球", list(last_draw[0]), "蓝球", int(next_draw[0]))
报错
Traceback (most recent call last):
  File "c:\Users\21897\Desktop\import random.py", line 28, in
    table = driver.find_element(By.XPATH, "//table[contains(@class, 'trend-table')]")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\21897\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 831, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\21897\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute      
    self.error_handler.check_response(response)
  File "C:\Users\21897\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//table[contains(@class, 'trend-table')]"}
  (Session info: MicrosoftEdge=113.0.1774.57)
Stacktrace:
Backtrace:
        GetHandleVerifier [0x00007FF635394B22+67490]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF63532B292+782498]
        (No symbol) [0x00007FF6350CC646]
        (No symbol) [0x00007FF63510F972]
        (No symbol) [0x00007FF63510FB8A]
        (No symbol) [0x00007FF635149817]
        (No symbol) [0x00007FF63512DF8F]
        (No symbol) [0x00007FF635103751]
        (No symbol) [0x00007FF635146BC5]
        (No symbol) [0x00007FF63512DD23]
        (No symbol) [0x00007FF635102794]
        (No symbol) [0x00007FF6351019B0]
        (No symbol) [0x00007FF635102F04]
        Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF635559123+1301571]
        (No symbol) [0x00007FF63518B951]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6352746B1+33985]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF63526CAE5+2293]
        Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF635557E83+1296803]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6353325D9+812009]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF63532EF54+798052]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF63532F04C+798300]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF635325101+757521]
        BaseThreadInitThunk [0x00007FFE1ED726AD+29]
        RtlUserThreadStart [0x00007FFE1F42AA08+40]

蓝球, 开奖号码

Maxhaha   

你直接post网页不行么。。。为啥还要创建Edge WebDriver实例呢。。。。post网页以后   你也可以获取到里面的代码的
您需要登录后才可以回帖 登录 | 立即注册

返回顶部