diff --git a/xiaohongshu/feed_detail.go b/xiaohongshu/feed_detail.go index db8a08a..2c996dd 100644 --- a/xiaohongshu/feed_detail.go +++ b/xiaohongshu/feed_detail.go @@ -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 { diff --git a/xiaohongshu/feeds.go b/xiaohongshu/feeds.go index 1cd3ed6..0110a25 100644 --- a/xiaohongshu/feeds.go +++ b/xiaohongshu/feeds.go @@ -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__) {