fork download
  1. # your code goes here
  2.  
Success #stdin #stdout 0.02s 7204KB
stdin
import requests
from bs4 import BeautifulSoup
import pandas as pd

# 定义目标URL
base_url = "https://w...content-available-to-author-only...v.cn/ygkj/wqkjgg/ssq/"

# 初始化数据存储
data = []

# 页码循环抓取
for page in range(1, 5):  # 修改范围以包含更多页
    url = f"{base_url}?page={page}"
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    
    # 查找数据表格
    table = soup.find("table")  # 根据网站表格结构调整选择器
    rows = table.find_all("tr")[1:]  # 跳过标题行

    for row in rows:
        cells = row.find_all("td")
        if len(cells) == 8:  # 确保字段数量正确
            period = cells[0].text.strip()  # 期号
            red_balls = [cells[i].text.strip() for i in range(1, 7)]  # 红球
            blue_ball = cells[7].text.strip()  # 蓝球
            data.append([period, *red_balls, blue_ball])

# 保存为CSV
columns = ['期号', '红球1', '红球2', '红球3', '红球4', '红球5', '红球6', '蓝球']
df = pd.DataFrame(data, columns=columns)
df.to_csv('ssq_results.csv', index=False, encoding='utf-8')

print("数据抓取完成,保存为 ssq_results.csv")
stdout
Standard output is empty