【Python爬虫】利用爬虫抓取双色球开奖号码,获取完整数据并通过随机森林和多层感知两种模型进行简单的预测

首先我们需要通过爬虫获取往期双色球号码并将其保存在csv文件中,这里我获取了1000期的数据,时间很久,大家可以修改代码少收集一些做尝试!

python 复制代码
import requests
import os
from bs4 import BeautifulSoup
import csv
import time

def download(url, page):
    while True:
        try:
            html = requests.get(url).text
            soup = BeautifulSoup(html, 'html.parser')
            list = soup.select('div.ball_box01 ul li')
            ball = []
            for li in list:
                ball.append(li.string)
            if not ball:
                raise ValueError("Empty data")
            write_to_excel(page, ball)
            print(f"第{page}期开奖结果录入完成")
            break
        except Exception as e:
            print(f"Attempt failed with error: {e}, retrying...")
            time.sleep(5)  # 等待5秒后重试

def write_to_excel(page, ball):
    with open('双色球开奖结果2.csv', 'a', encoding='utf_8_sig', newline='') as f:
        writer = csv.writer(f)
        writer.writerow([f'第{page}期'] + ball)

def turn_page():
    url = "https://kaijiang.500.com/ssq.shtml"
    html = requests.get(url).text
    soup = BeautifulSoup(html, 'html.parser')
    pageList = soup.select("div.iSelectList a")

    recent_pages = pageList[:1000]  # 获取最近1000期的页码

    for p in recent_pages:
        url = p['href']
        page = p.string
        download(url, page)

def main():
    if os.path.exists('双色球开奖结果2.csv'):
        os.remove('双色球开奖结果2.csv')
    turn_page()

if __name__ == '__main__':
    main()

这里是随机森林预测

python 复制代码
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor

# 读取数据
data = pd.read_csv('双色球开奖结果2.csv')  # ,encoding='gbk'

# 提取特征和标签
features = data.iloc[:, 1:7]  # 红色球特征
labels = data.iloc[:, 1:7]  # 红色球标签
blue_balls = data.iloc[:, 7]  # 蓝色球标签

# 创建随机森林回归模型
model = RandomForestRegressor(n_estimators=100, random_state=1)

# 拟合模型
model.fit(features, labels)

# 预测下一期的红色球号码
next_red_balls = model.predict(features.iloc[-1].values.reshape(1, -1))
next_red_balls = np.round(next_red_balls).astype(int)

# 预测下一期的蓝色球号码
blue_ball_model = RandomForestRegressor(n_estimators=100, random_state=1)
blue_ball_model.fit(features, blue_balls)
next_blue_ball = blue_ball_model.predict(features.iloc[-1].values.reshape(1, -1))
next_blue_ball = np.round(next_blue_ball).astype(int)

# 打印预测的红色球号码和蓝色球号码
print("预测的红色球号码:", next_red_balls)
print("预测的蓝色球号码:", next_blue_ball)

多层感知

python 复制代码
import pandas as pd
import numpy as np
from sklearn.neural_network import MLPRegressor

# 读取数据
data = pd.read_csv('双色球开奖结果2.csv')  # , encoding='gbk'

# 提取特征和标签
features = data.iloc[:, 1:7]  # 红色球特征
labels = data.iloc[:, 1:8]  # 红色球标签和蓝色球标签

# 创建多层感知机回归模型
model = MLPRegressor(hidden_layer_sizes=(100,), random_state=1)

# 拟合模型
model.fit(features, labels)
# 预测下一期的红色球号码和蓝色球号码
next_features = model.predict(features.iloc[[-1]])
next_features = np.round(next_features).astype(int)

# 打印预测的红色球号码和蓝色球号码
print("预测的红色球号码:", next_features[:6])
print("预测的蓝色球号码:", next_features[6])

杰哥这里仅做了简单的预测,闲暇时间无聊做的,大家想要更精确的结果需要更精细的调参!

相关推荐
2401_831501739 分钟前
Python学习之day03学习(文件和异常)
开发语言·python·学习
可触的未来,发芽的智生33 分钟前
触摸未来2025.10.06:声之密语从生理构造到神经网络的声音智能革命
人工智能·python·神经网络·机器学习·架构
Zwb29979234 分钟前
Day 24 - 文件、目录与路径 - Python学习笔记
笔记·python·学习
hui函数43 分钟前
python全栈(基础篇)——day03:后端内容(字符串格式化+简单数据类型转换+进制的转换+运算符+实战演示+每日一题)
开发语言·后端·python·全栈
动能小子ohhh1 小时前
AI智能体(Agent)大模型入门【6】--编写fasteAPI后端请求接口实现页面聊天
人工智能·python·深度学习·ai编程
SCBAiotAigc1 小时前
huggingface里的数据集如何下载呢?
人工智能·python
AntBlack1 小时前
PyQtInspect : 推荐一个好用的 PythonQT 界面 Debug 工具
python·pyqt
flashlight_hi1 小时前
LeetCode 分类刷题:1901. 寻找峰值 II
python·算法·leetcode
fwerfv3453452 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
蒋星熠2 小时前
反爬虫机制深度解析:从基础防御到高级对抗的完整技术实战
人工智能·pytorch·爬虫·python·深度学习·机器学习·计算机视觉