From 5912111f290948e6e964420ff83ae7bc541cedbb Mon Sep 17 00:00:00 2001 From: eson Date: Wed, 2 Sep 2020 19:18:44 +0800 Subject: [PATCH] add: WaitFor method add: Channel tag --- .../nimo_extractor/nimo_extractor_test.go | 35 ++----------------- utils.go | 29 +++++++++++++++ 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/extractor/nimo_extractor/nimo_extractor_test.go b/extractor/nimo_extractor/nimo_extractor_test.go index 61bc2fb..bec1c67 100644 --- a/extractor/nimo_extractor/nimo_extractor_test.go +++ b/extractor/nimo_extractor/nimo_extractor_test.go @@ -4,7 +4,6 @@ import ( "intimate" "log" "testing" - "time" "github.com/474420502/extractor" "github.com/tebeka/selenium" @@ -14,44 +13,16 @@ func TestMain(t *testing.T) { Execute() } -type WaitFor struct { - WebDriver selenium.WebDriver -} - -func NewWaitFor(wd selenium.WebDriver) *WaitFor { - return &WaitFor{WebDriver: wd} -} - -func (wf *WaitFor) Default(xpath string, do func(elements ...selenium.WebElement) bool) error { - return wf.WebDriver.WaitWithTimeout(func(wd selenium.WebDriver) (bool, error) { - elements, err := wd.FindElements(selenium.ByXPATH, xpath) - if err != nil { - log.Println(err) - return false, err - } - - if len(elements) > 0 { - if do == nil { - return true, nil - } - if do(elements...) { - return true, nil - } - } - return false, nil - - }, time.Second*15) -} - type LiveInfo struct { Followers int64 `exp:"//div[contains(@class,'nimo-rm_followers')]//span[@class='text c2']" mth:"r:ExtractNumber"` Views int64 `exp:"//div[contains(@class,'nimo-rm_audience')]//span[@class='text c2']" mth:"r:ExtractNumber"` + Channel string `exp:"//div[contains(@class,'nimo-rm_type')]//span"` Giver []int64 `exp:"//div[contains(@class,'rank-item-after3')]//span[contains(@class,'nimo-currency__count')]"` } func Execute() { wd := intimate.GetChromeDriver(3031) - waitfor := NewWaitFor(wd) + waitfor := intimate.NewWaitFor(wd) ps := intimate.NewPerfectShutdown() for !ps.IsClose() { @@ -78,8 +49,8 @@ func Execute() { log.Println(err) } } - waitfor.Default("//div[contains(@class,'nimo-rm_audience')]//span[@class='text c2']", nil) + pagesource, _ = wd.PageSource() etor := extractor.ExtractHtmlString(pagesource) li := etor.GetObjectByTag(LiveInfo{}).(*LiveInfo) diff --git a/utils.go b/utils.go index c3b9ac1..ee6a7d5 100644 --- a/utils.go +++ b/utils.go @@ -261,3 +261,32 @@ func (c *Counter) Add(n int) error { } return nil } + +type WaitFor struct { + WebDriver selenium.WebDriver +} + +func NewWaitFor(wd selenium.WebDriver) *WaitFor { + return &WaitFor{WebDriver: wd} +} + +func (wf *WaitFor) Default(xpath string, do func(elements ...selenium.WebElement) bool) error { + return wf.WebDriver.WaitWithTimeout(func(wd selenium.WebDriver) (bool, error) { + elements, err := wd.FindElements(selenium.ByXPATH, xpath) + if err != nil { + log.Println(err) + return false, err + } + + if len(elements) > 0 { + if do == nil { + return true, nil + } + if do(elements...) { + return true, nil + } + } + return false, nil + + }, time.Second*15) +}