From 90169ec894a4602c724c4c6a4135398007c7698a Mon Sep 17 00:00:00 2001 From: zy Date: Sun, 14 Sep 2025 21:47:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=B0=8F?= =?UTF-8?q?=E7=BA=A2=E4=B9=A6=E6=A0=87=E7=AD=BE=E8=BE=93=E5=85=A5=E9=80=BB?= =?UTF-8?q?=E8=BE=91=20(#71)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 优化小红书标签输入逻辑 - 调整 inputTag 函数以减少延迟时间 - 增加标签联想下拉框的等待时间 - 更新 MustInput 和 MustKeyActions 方法的调用顺序 - 添加错误处理和日志记录以增强稳定性 * update headless_browser version * downgrade fetchup version * update publish --- go.mod | 2 +- go.sum | 2 ++ xiaohongshu/publish.go | 29 ++++++++++++++++++++++------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index a1bf9fd..662121f 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.10.0 - github.com/xpzouying/headless_browser v0.1.0 + github.com/xpzouying/headless_browser v0.2.0 ) require ( diff --git a/go.sum b/go.sum index 6aa5250..f431496 100644 --- a/go.sum +++ b/go.sum @@ -83,6 +83,8 @@ github.com/xpzouying/headless_browser v0.0.2 h1:sLc4gqUT/5IyTruYIOfCW4aZLinq38hI github.com/xpzouying/headless_browser v0.0.2/go.mod h1:bQTSzGYHIipa1zwToMlOGHcXWDlvw8y33Cx5zzElekc= github.com/xpzouying/headless_browser v0.1.0 h1:0FyMIzhe/If/VhEdDrs7T1fqm1gOZSCFrmMXI/1JM58= github.com/xpzouying/headless_browser v0.1.0/go.mod h1:bQTSzGYHIipa1zwToMlOGHcXWDlvw8y33Cx5zzElekc= +github.com/xpzouying/headless_browser v0.2.0 h1:EmuHXDVzx0tAevHJUdETs8iT/eK+QqrLiybvGd1xZDA= +github.com/xpzouying/headless_browser v0.2.0/go.mod h1:bQTSzGYHIipa1zwToMlOGHcXWDlvw8y33Cx5zzElekc= github.com/ysmood/fetchup v0.2.3 h1:ulX+SonA0Vma5zUFXtv52Kzip/xe7aj4vqT5AJwQ+ZQ= github.com/ysmood/fetchup v0.2.3/go.mod h1:xhibcRKziSvol0H1/pj33dnKrYyI2ebIvz5cOOkYGns= github.com/ysmood/fetchup v0.5.2 h1:P9w3OIA7RSNEEFvEmOiTq09IOu42C96PMyZ1MWd8TAs= diff --git a/xiaohongshu/publish.go b/xiaohongshu/publish.go index c9af2f9..ae4e5cf 100644 --- a/xiaohongshu/publish.go +++ b/xiaohongshu/publish.go @@ -7,6 +7,7 @@ import ( "time" "github.com/go-rod/rod" + "github.com/go-rod/rod/lib/input" "github.com/go-rod/rod/lib/proto" "github.com/pkg/errors" ) @@ -113,6 +114,8 @@ func submitPublish(page *rod.Page, title, content string, tags []string) error { return errors.New("没有找到内容输入框") } + time.Sleep(1 * time.Second) + submitButton := page.MustElement("div.submit div.d-button-content") submitButton.MustClick() @@ -152,10 +155,23 @@ func inputTags(contentElem *rod.Element, tags []string) { return } - contentElem.MustInput("\n\n") + time.Sleep(1 * time.Second) + + for i := 0; i < 20; i++ { + contentElem.MustKeyActions(). + Type(input.ArrowRight). + MustDo() + time.Sleep(10 * time.Millisecond) + } + + contentElem.MustKeyActions(). + Press(input.Enter). + Press(input.Enter). + MustDo() + + time.Sleep(1 * time.Second) for _, tag := range tags { - tag = strings.TrimLeft(tag, "#") inputTag(contentElem, tag) } @@ -163,15 +179,14 @@ func inputTags(contentElem *rod.Element, tags []string) { func inputTag(contentElem *rod.Element, tag string) { contentElem.MustInput("#") - time.Sleep(200 * time.Millisecond) // 等待标签系统激活 + time.Sleep(200 * time.Millisecond) for _, char := range tag { contentElem.MustInput(string(char)) - time.Sleep(50 * time.Millisecond) // 减少延迟时间 + time.Sleep(50 * time.Millisecond) } - // 等待标签联想下拉框出现 - time.Sleep(500 * time.Millisecond) + time.Sleep(1 * time.Second) page := contentElem.Page() topicContainer, err := page.Element("#creator-editor-topic-container") @@ -180,7 +195,7 @@ func inputTag(contentElem *rod.Element, tag string) { if err == nil && firstItem != nil { firstItem.MustClick() slog.Info("成功点击标签联想选项", "tag", tag) - time.Sleep(200 * time.Millisecond) // 等待选择生效 + time.Sleep(200 * time.Millisecond) } else { slog.Warn("未找到标签联想选项,直接输入空格", "tag", tag) // 如果没有找到联想选项,输入空格结束