python在哪个网站可以下载棋谱,如何将中国象棋棋谱转换为moves.excel模式,用于模型训练。完整可使用的,不要理论的。
5条回答 默认 最新
关注让【宝码香车】来帮您解答,本回答参考DeepSeek编写,并整理提供。
如果答案让您满意,请采纳、点赞、关注,非常感谢!
.
每日一句正能量,让它带给您好心情:你所坚持的,终会反过来拥抱你。
.中国象棋棋谱下载与转换方案
一、棋谱下载网站
以下网站可下载中国象棋棋谱:
- 东萍象棋网 (http://www.dpxq.com/) - 包含大量棋谱数据库
- 象棋巫师 (http://www.chesswizard.cn/) - 提供棋谱下载和分析
- 广东象棋网 (http://www.gdchess.com/) - 丰富的棋谱资源
- 中国象棋大师网 (http://www.chessdb.cn/) - 专业棋谱数据库
推荐使用东萍象棋网,它提供API接口和批量下载功能。
二、完整可用的Python实现方案
1. 安装所需库
pip install requests beautifulsoup4 pandas openpyxl chess2. 从东萍象棋网下载棋谱的代码
import requests from bs4 import BeautifulSoup import pandas as pd import re import time def download_chess_records(page_count=10): """ 从东萍象棋网下载棋谱 page_count: 要下载的页数 """ base_url = "http://www.dpxq.com/hldcg/search/view_" records = [] for page in range(1, page_count + 1): try: url = f"{base_url}{page}.html" response = requests.get(url, timeout=10) response.encoding = 'gbk' # 东萍网使用GBK编码 soup = BeautifulSoup(response.text, 'html.parser') # 查找棋谱链接(实际选择器可能需要调整) links = soup.find_all('a', href=re.compile(r'view_\d+\.html')) for link in links: record_url = "http://www.dpxq.com" + link['href'] record_response = requests.get(record_url) record_response.encoding = 'gbk' # 解析棋谱内容 record_soup = BeautifulSoup(record_response.text, 'html.parser') xq_text = record_soup.find('textarea', {'id': 'xq_text'}) if xq_text: records.append({ 'title': link.text.strip(), 'content': xq_text.text.strip(), 'url': record_url }) print(f"已下载第 {page} 页,累计获取 {len(records)} 个棋谱") time.sleep(1) # 防止请求过于频繁 except Exception as e: print(f"下载第 {page} 页时出错: {e}") continue return records3. 棋谱转换器(转换为Excel格式)
def parse_chess_moves(chess_content): """ 解析中国象棋棋谱着法 """ moves = [] lines = chess_content.split('\n') for line in lines: line = line.strip() if re.match(r'^\d+\.', line): # 匹配着法行 # 移除步数编号 move_text = re.sub(r'^\d+\.\s*', '', line) # 分割红黑着法 parts = re.split(r'\s+', move_text) if len(parts) >= 2: moves.append({ 'red_move': parts[0], 'black_move': parts[1] if len(parts) > 1 else '' }) return moves def convert_to_excel(records, output_file='chess_moves.xlsx'): """ 将棋谱转换为Excel格式 """ all_data = [] for record in records: moves = parse_chess_moves(record['content']) for i, move in enumerate(moves): all_data.append({ 'game_id': record['title'], 'move_number': i + 1, 'red_move': move['red_move'], 'black_move': move['black_move'], 'source_url': record['url'] }) # 创建DataFrame并保存为Excel df = pd.DataFrame(all_data) df.to_excel(output_file, index=False, engine='openpyxl') print(f"已保存 {len(all_data)} 步着法到 {output_file}") return df # 完整的使用示例 if __name__ == "__main__": # 下载棋谱 print("开始下载棋谱...") chess_records = download_chess_records(page_count=5) # 下载5页作为示例 if chess_records: # 转换为Excel print("开始转换棋谱...") df = convert_to_excel(chess_records, 'chinese_chess_moves.xlsx') print("转换完成!") # 显示前几行数据 print("\n前5行数据预览:") print(df.head()) else: print("未下载到棋谱数据")4. 增强版解析器(处理更多格式)
def enhanced_parse_moves(chess_content): """ 增强版棋谱解析,处理更多格式 """ moves = [] # 处理常见的棋谱格式 patterns = [ r'(\d+)\.\s*([^\s]+)\s+([^\s]+)', # 标准格式: 1. 炮二平五 马8进7 r'(\d+)[\.\.]\s*([^\s]+)\s+([^\s]+)' # 中文标点格式 ] for pattern in patterns: matches = re.findall(pattern, chess_content) for match in matches: moves.append({ 'move_number': int(match[0]), 'red_move': match[1], 'black_move': match[2] }) return moves def batch_process_chess_records(records): """ 批量处理多个棋谱 """ processed_data = [] for record in records: try: moves = enhanced_parse_moves(record['content']) for move in moves: processed_data.append({ 'game_title': record['title'], 'move_number': move['move_number'], 'red_move': move['red_move'], 'black_move': move['black_move'], 'source': record['url'] }) except Exception as e: print(f"处理棋谱 {record['title']} 时出错: {e}") continue return processed_data5. 使用示例和输出
运行上述代码后,将生成一个Excel文件,包含以下列:
- game_id: 棋局标识
- move_number: 着法序号
- red_move: 红方着法
- black_move: 黑方着法
- source_url: 数据来源
三、注意事项
- 网站政策: 下载前请遵守网站的robots.txt和使用条款
- 请求频率: 添加适当的延迟,避免对服务器造成压力
- 编码问题: 中文网站可能使用GBK编码,需要正确设置
- 格式变化: 不同网站的棋谱格式可能不同,需要调整解析逻辑
四、扩展功能建议
如需进一步处理,可以考虑:
- 添加棋局元信息(棋手、比赛、日期等)
- 实现着法标准化(统一 notation)
- 添加棋局结果信息
- 实现批量下载和增量更新
这个方案提供了完整的从下载到转换的流程,可以直接用于模型训练的数据准备。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报