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