From 34ddea1047cbe42d37b5feb5762cf0bd428789e6 Mon Sep 17 00:00:00 2001 From: SchwarzSail <1424928981@qq.com> Date: Sat, 11 Jan 2025 21:18:07 +0800 Subject: [PATCH] add pageNum as value returned --- jwch_test.go | 5 +++-- notice.go | 32 ++++++++++++++++---------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/jwch_test.go b/jwch_test.go index 08068e1..03b10f0 100644 --- a/jwch_test.go +++ b/jwch_test.go @@ -279,11 +279,12 @@ func TestGetExamRoomInfo(t *testing.T) { } func TestGetNoticesInfo(t *testing.T) { - content, err := stu.GetNoticeInfo(&NoticeInfoReq{PageNum: 2}) + content, totalPages, err := stu.GetNoticeInfo(&NoticeInfoReq{PageNum: 2}) + fmt.Println(totalPages) if err != nil { t.Error(err) } - if content == nil { + if content == nil || totalPages == 0 { t.Error("content is nil") } } diff --git a/notice.go b/notice.go index 0bf0e80..8a2eae2 100644 --- a/notice.go +++ b/notice.go @@ -26,42 +26,42 @@ import ( "github.com/west2-online/jwch/constants" ) -func (s *Student) GetNoticeInfo(req *NoticeInfoReq) (list []*NoticeInfo, err error) { +func (s *Student) GetNoticeInfo(req *NoticeInfoReq) (list []*NoticeInfo, totalPages int, err error) { // 获取通知公告页面的总页数 res, err := s.PostWithIdentifier(constants.NoticeInfoQueryURL, map[string]string{}) if err != nil { - return nil, err + return nil, 0, err } - // 首页直接爬取 - if req.PageNum == 1 { - list, err = parseNoticeInfo(res) - if err != nil { - return nil, err - } - return list, nil - } - // 分页需要根据页数计算 url + // 获取总页数 lastPageNum, err := getTotalPages(res) if err != nil { - return nil, err + return nil, 0, err } // 判断是否超出总页数 if req.PageNum > lastPageNum { - return nil, fmt.Errorf("超出总页数") + return nil, lastPageNum, fmt.Errorf("超出总页数") + } + // 首页直接爬取 + if req.PageNum == 1 { + list, err = parseNoticeInfo(res) + if err != nil { + return nil, lastPageNum, err + } + return list, lastPageNum, nil } // 根据总页数计算 url num := lastPageNum - req.PageNum + 1 url := fmt.Sprintf("https://jwch.fzu.edu.cn/jxtz/%d.htm", num) doc, err := s.PostWithIdentifier(url, map[string]string{}) if err != nil { - return nil, err + return nil, lastPageNum, err } list, err = parseNoticeInfo(doc) if err != nil { - return nil, err + return nil, lastPageNum, err } // 3. 返回结果 - return list, nil + return list, lastPageNum, nil } // 获取当前页面的所有数据信息