update wait for list feed and get feed details (#52)

更新一下 list-feed,get-feed-detail 行为的等待,
这里可能是 windows 的问题所在
This commit is contained in:
zy
2025-09-10 00:30:31 +08:00
committed by GitHub
parent 8338252ab5
commit f21183fa48
2 changed files with 5 additions and 11 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/go-rod/rod"
@@ -29,8 +28,8 @@ func (f *FeedDetailAction) GetFeedDetail(ctx context.Context, feedID, xsecToken
// 导航到详情页
page.MustNavigate(url)
page.MustWaitStable()
page.MustWait(`() => window.__INITIAL_STATE__ !== undefined`)
page.MustWaitDOMStable()
time.Sleep(1 * time.Second)
// 获取 window.__INITIAL_STATE__ 并转换为 JSON 字符串
result := page.MustEval(`() => {
@@ -44,12 +43,6 @@ func (f *FeedDetailAction) GetFeedDetail(ctx context.Context, feedID, xsecToken
return nil, fmt.Errorf("__INITIAL_STATE__ not found")
}
// 将原始结果保存到 feed_detail.json 文件用于测试
err := os.WriteFile("feed_detail.json", []byte(result), 0644)
if err != nil {
return nil, fmt.Errorf("failed to write feed_detail.json: %w", err)
}
// 定义响应结构并直接反序列化
var initialState struct {
Note struct {

View File

@@ -22,8 +22,7 @@ func NewFeedsListAction(page *rod.Page) *FeedsListAction {
pp := page.Timeout(60 * time.Second)
pp.MustNavigate("https://www.xiaohongshu.com")
pp.MustWaitStable()
pp.MustWait(`() => window.__INITIAL_STATE__ !== undefined`)
pp.MustWaitDOMStable()
return &FeedsListAction{page: pp}
}
@@ -32,6 +31,8 @@ func NewFeedsListAction(page *rod.Page) *FeedsListAction {
func (f *FeedsListAction) GetFeedsList(ctx context.Context) ([]Feed, error) {
page := f.page.Context(ctx)
time.Sleep(1 * time.Second)
// 获取 window.__INITIAL_STATE__ 并转换为 JSON 字符串
result := page.MustEval(`() => {
if (window.__INITIAL_STATE__) {