C#调用基于python打包的税务申报期限解析器exe工具
税务申报期限解析器
自动从地市官网抓取并解析年度纳税申报期限的Python工具。
功能特点
- 自动从地市官网抓取最新的年度申报纳税期限通知
- 解析网页内容提取各月份的纳税申报截止日期
- 生成结构化的XML文件便于后续处理
- 支持命令行参数配置输出文件名
工作原理
- 通过POST请求获取税务局网站的纳税期限通知列表
- 在列表中查找包含"年度申报纳税期限的通知"的条目
- 进入详情页并提取网页内容
- 从网页标题中提取年份信息
- 解析
mainText
区域的第二个div中的文本内容 - 使用正则表达式识别各月份的申报截止日期
- 生成包含所有月份的完整纳税期限信息XML文件
安装依赖
pip install requests beautifulsoup4 lxml
使用方法
基本使用
python tax_deadline_parser.py
打包exe
pip install pyinstallerpyinstaller --onefile --console --clean --noconfirm tax_deadline_parser.py
指定输出文件
python tax_deadline_parser.py -o my_tax_deadlines.xml
命令行参数
-o, --output
: 指定输出XML文件名(默认为tax_deadlines_out.xml
)
输出格式
生成的XML文件包含以下结构:
<TaxDeadlines year="2024" generated_date="2024-01-01T00:00:00" source="国家税务总局官网"><Deadline><Year>2024</Year><Month>1</Month><DeadlineDate>1月15日</DeadlineDate><Note>默认申报期限</Note></Deadline><!-- 更多月份数据 -->
</TaxDeadlines>
错误处理
程序具有良好的错误处理机制:
- 网络请求失败时会重试或报错
- 无法解析网页内容时会使用默认值
- 任何异常都会输出错误信息并退出
注意事项
- 该工具专门针对地市官网网站结构设计
- 如果网站结构发生变化,可能需要调整解析逻辑
- 需要网络连接以访问地市官网
- 生成的XML文件默认保存在程序同目录下
C#调用示例
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;namespace IM.Common
{//xml公共处理类public class XmlHelper{public static bool GenerateXmlFile(string exePath, string outputFile){if (!File.Exists(exePath)){LogHelper.WriteLog($"错误: 找不到可执行文件 {exePath}");return false;}try{// 删除已存在的文件if (File.Exists(outputFile)){File.Delete(outputFile);LogHelper.WriteLog($"已删除旧文件: {outputFile}");}string arguments = $"-o \"{outputFile}\"";ProcessStartInfo startInfo = new ProcessStartInfo{FileName = exePath,Arguments = arguments,UseShellExecute = false,RedirectStandardOutput = true,RedirectStandardError = true,CreateNoWindow = true,StandardOutputEncoding = Encoding.UTF8,StandardErrorEncoding = Encoding.UTF8};LogHelper.WriteLog("正在生成XML文件...");using (Process process = new Process()){process.StartInfo = startInfo;process.Start();// 读取输出string output = process.StandardOutput.ReadToEnd();string error = process.StandardError.ReadToEnd();process.WaitForExit(60000);if (process.ExitCode == 0 && File.Exists(outputFile)){LogHelper.WriteLog("✅ XML文件生成成功!");LogHelper.WriteLog($"📁 文件位置: {Path.GetFullPath(outputFile)}");return true;}else{LogHelper.WriteLog($"❌ XML文件生成失败: {error}");return false;}}}catch (Exception ex){LogHelper.WriteLog($"生成XML文件时出错: {ex.Message}");return false;}}/// <summary>/// 读取XML文件内容/// </summary>public static string ReadXmlFile(string xmlFilePath){try{if (!File.Exists(xmlFilePath)){LogHelper.WriteLog($"错误: XML文件不存在: {xmlFilePath}");return null;}string xmlContent = File.ReadAllText(xmlFilePath, Encoding.UTF8);LogHelper.WriteLog($"✅ XML文件读取成功,内容长度: {xmlContent.Length} 字符");// 验证XML格式if (ValidateXml(xmlContent)){return xmlContent;}else{LogHelper.WriteLog("❌ XML格式验证失败");return null;}}catch (Exception ex){LogHelper.WriteLog($"读取XML文件时出错: {ex.Message}");return null;}}/// <summary>/// 验证XML格式/// </summary>public static bool ValidateXml(string xmlContent){try{XmlDocument xmlDoc = new XmlDocument();xmlDoc.LoadXml(xmlContent);return true;}catch (Exception ex){LogHelper.WriteLog($"XML验证错误: {ex.Message}");return false;}} }
}
//调用方法
public void excuteGenerate(){// Python脚本路径string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaxDeadLine", "tax_deadline_parser.exe");if (!File.Exists(exePath)){res.Code = "错误码";res.Message = $"错误: 找不到可执行文件 {exePath}";LogHelper.WriteLog($"错误: 找不到可执行文件 {exePath}");return res;}// 输出文件路径string outputFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaxDeadLine", "tax_deadlines_output.xml"); // 1. 生成XML文件if (XmlHelper.GenerateXmlFile(exePath, outputFile)){// 2. 读取XML内容string xmlContent = XmlHelper.ReadXmlFile(outputFile);if (!string.IsNullOrEmpty(xmlContent)){// 3. 调用存储过程 存储数据到数据库res.Code = 200;res.Message = "读取XML内容失败";return res;}else{res.Code = 500;res.Message = "读取XML内容失败";LogHelper.WriteLog($"读取XML内容失败,请检查xml所在文件路径:{outputFile}下是否包含【tax_deadlines_output.xml】文件");return res;}}else{res.Code = "错误码";res.Message = "生成XML文件失败";LogHelper.WriteLog($"生成XML文件失败");return res;}}
开发依赖
- Python 3.6+
- requests: 网页请求库
- beautifulsoup4: HTML解析库
- lxml: XML处理库