update wait for list feed and get feed details (#52)
更新一下 list-feed,get-feed-detail 行为的等待, 这里可能是 windows 的问题所在
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-rod/rod"
|
"github.com/go-rod/rod"
|
||||||
@@ -29,8 +28,8 @@ func (f *FeedDetailAction) GetFeedDetail(ctx context.Context, feedID, xsecToken
|
|||||||
|
|
||||||
// 导航到详情页
|
// 导航到详情页
|
||||||
page.MustNavigate(url)
|
page.MustNavigate(url)
|
||||||
page.MustWaitStable()
|
page.MustWaitDOMStable()
|
||||||
page.MustWait(`() => window.__INITIAL_STATE__ !== undefined`)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
// 获取 window.__INITIAL_STATE__ 并转换为 JSON 字符串
|
// 获取 window.__INITIAL_STATE__ 并转换为 JSON 字符串
|
||||||
result := page.MustEval(`() => {
|
result := page.MustEval(`() => {
|
||||||
@@ -44,12 +43,6 @@ func (f *FeedDetailAction) GetFeedDetail(ctx context.Context, feedID, xsecToken
|
|||||||
return nil, fmt.Errorf("__INITIAL_STATE__ not found")
|
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 {
|
var initialState struct {
|
||||||
Note struct {
|
Note struct {
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ func NewFeedsListAction(page *rod.Page) *FeedsListAction {
|
|||||||
pp := page.Timeout(60 * time.Second)
|
pp := page.Timeout(60 * time.Second)
|
||||||
|
|
||||||
pp.MustNavigate("https://www.xiaohongshu.com")
|
pp.MustNavigate("https://www.xiaohongshu.com")
|
||||||
pp.MustWaitStable()
|
pp.MustWaitDOMStable()
|
||||||
pp.MustWait(`() => window.__INITIAL_STATE__ !== undefined`)
|
|
||||||
|
|
||||||
return &FeedsListAction{page: pp}
|
return &FeedsListAction{page: pp}
|
||||||
}
|
}
|
||||||
@@ -32,6 +31,8 @@ func NewFeedsListAction(page *rod.Page) *FeedsListAction {
|
|||||||
func (f *FeedsListAction) GetFeedsList(ctx context.Context) ([]Feed, error) {
|
func (f *FeedsListAction) GetFeedsList(ctx context.Context) ([]Feed, error) {
|
||||||
page := f.page.Context(ctx)
|
page := f.page.Context(ctx)
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
// 获取 window.__INITIAL_STATE__ 并转换为 JSON 字符串
|
// 获取 window.__INITIAL_STATE__ 并转换为 JSON 字符串
|
||||||
result := page.MustEval(`() => {
|
result := page.MustEval(`() => {
|
||||||
if (window.__INITIAL_STATE__) {
|
if (window.__INITIAL_STATE__) {
|
||||||
|
|||||||
Reference in New Issue
Block a user