first commit

This commit is contained in:
mei
2026-07-13 21:16:06 +08:00
commit 9420582bd0
7 changed files with 517 additions and 0 deletions
+143
View File
@@ -0,0 +1,143 @@
// package main
// import (
// "encoding/json"
// "fmt"
// "io"
// "log"
// "net/http"
// "strings"
// // 使用纯 Go 的 SQLite 驱动,免除 CGO 和 GCC 依赖报错
// "github.com/glebarez/sqlite"
// "gorm.io/gorm"
// )
// // ---- 1. 定义读取 JSON 用的结构体 ----
// type JSONResponse struct {
// ResponseCode string `json:"responseCode"`
// ResponseData RespData `json:"responseData"`
// }
// type RespData struct {
// Datas []QuestionData `json:"datas"`
// }
// type QuestionData struct {
// QueId int `json:"queId"`
// Stem string `json:"stem"` // 单选题将 stem 存为 nr
// QuestionAnswers []QuestionAnswer `json:"questionAnswers"`
// }
// type QuestionAnswer struct {
// AnswerDesc string `json:"answerDesc"`
// IsCorrect int `json:"isCorrect"`
// }
// // ---- 2. 定义对应 SQLite 的 GORM 模型 ----
// type Xuanze struct {
// ID int `gorm:"column:id;primaryKey"`
// Nr string `gorm:"column:nr"`
// Da string `gorm:"column:da"`
// }
// // TableName 指定绑定的表名
// func (Xuanze) TableName() string {
// return "xuanze"
// }
// func main() {
// // ==================== 第一步:发送单选题的网络请求 ====================
// client := &http.Client{}
// // 这里更新了你的单选题 data 参数
// var data = strings.NewReader(`rows=200&page=1&ownerId=102404796&btId=102404796&queTypeCode=SC&clickQueId=240677025`)
// req, err := http.NewRequest("POST", "https://sia.sinopec.com/ac/seiPracController/querySeiPracQuestionForPage.do", data)
// if err != nil {
// log.Fatalf("创建请求失败: %v", err)
// }
// // 设置你最新的 Headers 和 Cookie
// req.Header.Set("Accept", "application/json, text/plain, */*")
// req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
// req.Header.Set("Access-Control-Allow-Origin", "*")
// req.Header.Set("Authorization", "00000-181M")
// req.Header.Set("Connection", "keep-alive")
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
// req.Header.Set("Origin", "https://sia.sinopec.com")
// req.Header.Set("Referer", "https://sia.sinopec.com/mobile/")
// req.Header.Set("Sec-Fetch-Dest", "empty")
// req.Header.Set("Sec-Fetch-Mode", "cors")
// req.Header.Set("Sec-Fetch-Site", "same-origin")
// req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0")
// req.Header.Set("sec-ch-ua", `"Not;A=Brand";v="8", "Chromium";v="150", "Microsoft Edge";v="150"`)
// req.Header.Set("sec-ch-ua-mobile", "?0")
// req.Header.Set("sec-ch-ua-platform", `"Windows"`)
// req.Header.Set("Cookie", "accessId=839d6250-5041-11ee-96f3-ef2d7900d919; x-forwarded-for=null; zshIo=https%3A%2F%2Fsia.sinopec.com%2Fmobile%2F%23%2Fapp%2Fautonomous%2Ftest%2Fpractice%2Ftopics%3FqbId%3D263%26titleName%3D7%25E7%259F%25B3%25E5%25B7%25A5%25E5%25BB%25BA%252F%25E4%25B8%2593%25E4%25B8%259A%25E7%259F%25A5%25E8%25AF%2586%26domainId%3D6026f6138212ebda9f65e27591ac399f; types=0; Admin-Token=00000-181M; LEARNSSIONID=ccdc9e86-f458-472e-a31c-5ff82876f93f; pageViewNum=4; SERVERID=08454d75353b657bc1b9c1181db254db|1783943904|1783941591")
// resp, err := client.Do(req)
// if err != nil {
// log.Fatalf("发送请求失败: %v", err)
// }
// defer resp.Body.Close()
// bodyText, err := io.ReadAll(resp.Body)
// if err != nil {
// log.Fatalf("读取响应失败: %v", err)
// }
// // ==================== 第二步:解析返回的 JSON ====================
// var apiResponse JSONResponse
// err = json.Unmarshal(bodyText, &apiResponse)
// if err != nil {
// log.Fatalf("JSON 解析失败: %v", err)
// }
// // ==================== 第三步:连接数据库并写入 ====================
// db, err := gorm.Open(sqlite.Open("osecdtzx_question.db"), &gorm.Config{})
// if err != nil {
// log.Fatalf("数据库连接失败: %v", err)
// }
// // 自动检查并迁移表结构
// db.AutoMigrate(&Xuanze{})
// // 准备批量插入的数据切片
// var rowsToInsert []Xuanze
// for _, item := range apiResponse.ResponseData.Datas {
// // 1. 寻找正确答案文本 (isCorrect == 1)
// daText := ""
// for _, ans := range item.QuestionAnswers {
// if ans.IsCorrect == 1 {
// daText = ans.AnswerDesc
// break // 找到单选正确答案,跳出当前题目的选项循环
// }
// }
// // 2. 清洗可能残存的二次转义字符
// cleanStem := strings.ReplaceAll(item.Stem, `\n`, "\n")
// cleanStem = strings.ReplaceAll(cleanStem, `\r\n`, "\n")
// // 3. 构建模型
// row := Xuanze{
// ID: item.QueId, // 对应 id 字段
// Nr: cleanStem, // 单选题 nr 对应 stem
// Da: daText, // da 对应正确选项内容
// }
// rowsToInsert = append(rowsToInsert, row)
// }
// // 执行批量存入
// if len(rowsToInsert) > 0 {
// // 使用 db.Save 可以在遇到重复 id 时做“更新”操作,防止主键冲突报错
// result := db.Save(&rowsToInsert)
// if result.Error != nil {
// log.Fatalf("数据保存进数据库失败: %v", result.Error)
// }
// fmt.Printf("🎉 单选题处理成功!已写入/更新 %d 条题目数据。\n", result.RowsAffected)
// } else {
// fmt.Println("⚠️ 未在接口返回中解析到有效的单选题数据。")
// }
// }
+24
View File
@@ -0,0 +1,24 @@
module git.mmeiblog.cn/mei/ttt
go 1.24.0
require (
github.com/glebarez/sqlite v1.11.0
gorm.io/gorm v1.31.2
)
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.20.0 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/sqlite v1.23.1 // indirect
)
+38
View File
@@ -0,0 +1,38 @@
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo=
gorm.io/gorm v1.31.2/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM=
modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk=
+169
View File
@@ -0,0 +1,169 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
// 使用纯 Go 的 SQLite 驱动,免除 CGO 依赖
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
// ---- 1. 定义读取 JSON 用的结构体 ----
type JSONResponse struct {
ResponseCode string `json:"responseCode"`
ResponseData RespData `json:"responseData"`
}
type RespData struct {
Datas []QuestionData `json:"datas"`
}
type QuestionData struct {
QueId int `json:"queId"`
Stem string `json:"stem"`
QuestionAnswers []QuestionAnswer `json:"questionAnswers"`
}
type QuestionAnswer struct {
AnswerDesc string `json:"answerDesc"`
IsCorrect int `json:"isCorrect"`
}
// ---- 2. 定义对应 SQLite 的多选题 GORM 模型 ----
type Duoxuan struct {
ID int `gorm:"column:id;primaryKey"`
Nr string `gorm:"column:nr"`
Da1 *string `gorm:"column:da_1"` // 使用指针类型,当值为 nil 时,写入数据库就是 NULL
Da2 *string `gorm:"column:da_2"`
Da3 *string `gorm:"column:da_3"`
Da4 *string `gorm:"column:da_4"`
}
// TableName 指定绑定的表名为 duoxuan
func (Duoxuan) TableName() string {
return "duoxuan"
}
func main() {
// ==================== 第一步:发送多选题的网络请求 ====================
client := &http.Client{}
var data = strings.NewReader(`rows=200&page=1&ownerId=229&btId=102404796&queTypeCode=MC&clickQueId=240677024`)
req, err := http.NewRequest("POST", "https://sia.sinopec.com/ac/seiPracController/querySeiPracQuestionForPage.do", data)
if err != nil {
log.Fatalf("创建请求失败: %v", err)
}
// 设置 Headers 和 Cookie
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
req.Header.Set("Access-Control-Allow-Origin", "*")
req.Header.Set("Authorization", "00000-181M")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
req.Header.Set("Origin", "https://sia.sinopec.com")
req.Header.Set("Referer", "https://sia.sinopec.com/mobile/")
req.Header.Set("Sec-Fetch-Dest", "empty")
req.Header.Set("Sec-Fetch-Mode", "cors")
req.Header.Set("Sec-Fetch-Site", "same-origin")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0")
req.Header.Set("sec-ch-ua", `"Not;A=Brand";v="8", "Chromium";v="150", "Microsoft Edge";v="150"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
req.Header.Set("Cookie", "accessId=839d6250-5041-11ee-96f3-ef2d7900d919; x-forwarded-for=null; zshIo=https%3A%2F%2Fsia.sinopec.com%2Fmobile%2F%23%2Fapp%2Fautonomous%2Ftest%2Fpractice%2Ftopics%3FqbId%3D263%26titleName%3D7%25E7%259F%25B3%25E5%25B7%25A5%25E5%25BB%25BA%252F%25E4%25B8%2593%25E4%25B8%259A%25E7%259F%25A5%25E8%25AF%2586%26domainId%3D6026f6138212ebda9f65e27591ac399f; types=0; Admin-Token=00000-181M; LEARNSSIONID=ccdc9e86-f458-472e-a31c-5ff82876f93f; pageViewNum=4; SERVERID=08454d75353b657bc1b9c1181db254db|1783945192|1783941591")
resp, err := client.Do(req)
if err != nil {
log.Fatalf("发送请求失败: %v", err)
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("读取响应失败: %v", err)
}
// ==================== 第二步:解析返回的 JSON ====================
var apiResponse JSONResponse
err = json.Unmarshal(bodyText, &apiResponse)
if err != nil {
log.Fatalf("JSON 解析失败: %v", err)
}
// ==================== 第三步:连接数据库并写入 ====================
db, err := gorm.Open(sqlite.Open("osecdtzx_question.db"), &gorm.Config{})
if err != nil {
log.Fatalf("数据库连接失败: %v", err)
}
// 自动检查并迁移表结构
db.AutoMigrate(&Duoxuan{})
// 准备批量插入的数据切片
var rowsToInsert []Duoxuan
for _, item := range apiResponse.ResponseData.Datas {
// 1. 收集当前题目所有正确的答案
var correctAnswers []string
for _, ans := range item.QuestionAnswers {
if ans.IsCorrect == 1 {
correctAnswers = append(correctAnswers, ans.AnswerDesc)
}
}
// 2. 将正确的答案分配到 da_1, da_2, da_3, da_4 指针字段
// 默认都保持为 nil,对应的就是数据库里的 NULL
var d1, d2, d3, d4 *string
// 使用临时变量获取地址,防止迭代循环中的地址复用
if len(correctAnswers) > 0 {
val := correctAnswers[0]
d1 = &val
}
if len(correctAnswers) > 1 {
val := correctAnswers[1]
d2 = &val
}
if len(correctAnswers) > 2 {
val := correctAnswers[2]
d3 = &val
}
if len(correctAnswers) > 3 {
val := correctAnswers[3]
d4 = &val
}
// 3. 清洗可能残存的二次转义换行字符
cleanStem := strings.ReplaceAll(item.Stem, `\n`, "\n")
cleanStem = strings.ReplaceAll(cleanStem, `\r\n`, "\n")
// 4. 构建多选题模型
row := Duoxuan{
ID: item.QueId,
Nr: cleanStem,
Da1: d1,
Da2: d2,
Da3: d3,
Da4: d4,
}
rowsToInsert = append(rowsToInsert, row)
}
// 执行批量存入
if len(rowsToInsert) > 0 {
// db.Save 在执行具有 NULL 值的更新和插入时也完美支持
result := db.Save(&rowsToInsert)
if result.Error != nil {
log.Fatalf("数据保存进数据库失败: %v", result.Error)
}
fmt.Printf("🎉 多选题处理成功!已写入/更新 %d 条题目数据(空缺答案已被留空为 NULL)。\n", result.RowsAffected)
} else {
fmt.Println("⚠️ 未在接口返回中解析到有效的多选题数据。")
}
}
Binary file not shown.
+143
View File
@@ -0,0 +1,143 @@
// package main
// import (
// "encoding/json"
// "fmt"
// "io"
// "log"
// "net/http"
// "strings"
// "github.com/glebarez/sqlite"
// "gorm.io/gorm"
// )
// // ---- 1. 定义读取 JSON 用的结构体 ----
// type JSONResponse struct {
// ResponseCode string `json:"responseCode"`
// ResponseData RespData `json:"responseData"`
// }
// type RespData struct {
// Datas []QuestionData `json:"datas"`
// }
// type QuestionData struct {
// QueId int `json:"queId"`
// AnswerAnaly string `json:"answerAnaly"`
// Stem string `json:"stem"`
// QuestionAnswers []QuestionAnswer `json:"questionAnswers"`
// }
// type QuestionAnswer struct {
// AnswerDesc string `json:"answerDesc"`
// IsCorrect int `json:"isCorrect"`
// }
// // ---- 2. 定义对应 SQLite 的 GORM 模型 ----
// type Xuanze struct {
// ID int `gorm:"column:id;primaryKey"`
// Nr string `gorm:"column:nr"`
// Da string `gorm:"column:da"`
// }
// // TableName 指定绑定的表名
// func (Xuanze) TableName() string {
// return "xuanze"
// }
// func main() {
// // ==================== 第一步:发送网络请求获取数据 ====================
// client := &http.Client{}
// var data = strings.NewReader(`rows=200&page=1&ownerId=229&btId=102404796&queTypeCode=JQ&queryType=init`)
// req, err := http.NewRequest("POST", "https://sia.sinopec.com/ac/seiPracController/querySeiPracQuestionForPage.do", data)
// if err != nil {
// log.Fatalf("创建请求失败: %v", err)
// }
// // 设置你提供的 Headers
// req.Header.Set("Accept", "application/json, text/plain, */*")
// req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
// req.Header.Set("Access-Control-Allow-Origin", "*")
// req.Header.Set("Authorization", "00000-181M")
// req.Header.Set("Connection", "keep-alive")
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
// req.Header.Set("Origin", "https://sia.sinopec.com")
// req.Header.Set("Referer", "https://sia.sinopec.com/mobile/")
// req.Header.Set("Sec-Fetch-Dest", "empty")
// req.Header.Set("Sec-Fetch-Mode", "cors")
// req.Header.Set("Sec-Fetch-Site", "same-origin")
// req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0")
// req.Header.Set("sec-ch-ua", `"Not;A=Brand";v="8", "Chromium";v="150", "Microsoft Edge";v="150"`)
// req.Header.Set("sec-ch-ua-mobile", "?0")
// req.Header.Set("sec-ch-ua-platform", `"Windows"`)
// req.Header.Set("Cookie", "qimo_seosource_839d6250-5041-11ee-96f3-ef2d7900d919=%E7%AB%99%E5%86%85; qimo_seokeywords_839d6250-5041-11ee-96f3-ef2d7900d919=; accessId=839d6250-5041-11ee-96f3-ef2d7900d919; x-forwarded-for=null; zshIo=https%3A%2F%2Fsia.sinopec.com%2Fmobile%2F%23%2Fapp%2Fautonomous%2Ftest%2Fpractice%2Ftopics%3FqbId%3D263%26titleName%3D7%25E7%259F%25B3%25E5%25B7%25A5%25E5%25BB%25BA%252F%25E4%25B8%2593%25E4%25B8%259A%25E7%259F%25A5%25E8%25AF%2586%26domainId%3D6026f6138212ebda9f65e27591ac399f; types=0; Admin-Token=00000-181M; LEARNSSIONID=ccdc9e86-f458-472e-a31c-5ff82876f93f; SERVERID=08454d75353b657bc1b9c1181db254db|1783941689|1783941591; pageViewNum=4")
// resp, err := client.Do(req)
// if err != nil {
// log.Fatalf("发送请求失败: %v", err)
// }
// defer resp.Body.Close()
// bodyText, err := io.ReadAll(resp.Body)
// if err != nil {
// log.Fatalf("读取响应失败: %v", err)
// }
// // ==================== 第二步:解析返回的 JSON ====================
// var apiResponse JSONResponse
// err = json.Unmarshal(bodyText, &apiResponse)
// if err != nil {
// log.Fatalf("JSON 解析失败: %v. 原始返回内容: %s", err, string(bodyText))
// }
// // ==================== 第三步:连接数据库并写入 ====================
// // 连接本地 SQLite 数据库,这里更名为 "question.db",如果不存在会自动在当前目录下创建
// db, err := gorm.Open(sqlite.Open("osecdtzx_question.db"), &gorm.Config{})
// if err != nil {
// log.Fatalf("数据库连接失败: %v", err)
// }
// // 自动检查并迁移表结构(如果数据库没有 xuanze 表会自动建表)
// db.AutoMigrate(&Xuanze{})
// // 准备批量插入的数据切片
// var rowsToInsert []Xuanze
// for _, item := range apiResponse.ResponseData.Datas {
// // 转换答案映射逻辑
// daText := "未知"
// if len(item.QuestionAnswers) > 0 {
// switch item.QuestionAnswers[0].AnswerDesc {
// case "N":
// daText = "错误"
// case "Y":
// daText = "正确"
// }
// }
// cleanNr := strings.ReplaceAll(item.Stem, `\n`, "\n")
// cleanNr = strings.ReplaceAll(cleanNr, `\r\n`, "\n")
// // 构建 GORM 模型实例
// row := Xuanze{
// ID: item.QueId,
// Nr: cleanNr, // 放入清洗后的标准换行文本
// Da: daText,
// }
// rowsToInsert = append(rowsToInsert, row)
// }
// // 执行批量存入
// if len(rowsToInsert) > 0 {
// // 使用 db.Create 批量写入
// result := db.Create(&rowsToInsert)
// if result.Error != nil {
// log.Fatalf("数据保存进数据库失败: %v", result.Error)
// }
// fmt.Printf("🎉 请求成功!已成功将 %d 条题目数据写入 SQLite 数据库。\n", result.RowsAffected)
// } else {
// fmt.Println("⚠️ 未在接口返回中解析到题目数据。")
// }
// }
BIN
View File
Binary file not shown.