refactor: 优化小红书标签输入逻辑 (#71)
* refactor: 优化小红书标签输入逻辑 - 调整 inputTag 函数以减少延迟时间 - 增加标签联想下拉框的等待时间 - 更新 MustInput 和 MustKeyActions 方法的调用顺序 - 添加错误处理和日志记录以增强稳定性 * update headless_browser version * downgrade fetchup version * update publish
This commit is contained in:
2
go.mod
2
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 (
|
||||
|
||||
2
go.sum
2
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=
|
||||
|
||||
@@ -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)
|
||||
// 如果没有找到联想选项,输入空格结束
|
||||
|
||||
Reference in New Issue
Block a user