Files

144 lines
5.3 KiB
Plaintext
Raw Permalink Normal View History

2026-07-13 21:16:06 +08:00
// 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("⚠️ 未在接口返回中解析到题目数据。")
// }
// }