用rod获取百度搜索结果的例子

ELEMENTS爱乐小超 2020-05-07

package main

import (
    "fmt"
    "time"

    "github.com/ysmood/rod"
    "github.com/ysmood/rod/lib/input"
    "github.com/ysmood/rod/lib/launcher"
)

func Example_reuse_sessions() {
    url := launcher.NewUserMode().Launch()

    browser := rod.New().ControlURL(url).Connect()

    page := browser.Timeout(time.Second * 15).Page("https://www.baidu.com")
    page.Window(0, 0, 1920, 1080)
    // use css selector to get the search input element and input "git"
    page.Element("#kw").Input("测试").Press(input.Enter)
    // wait until css selector get the element then get the text content of it
    text := page.Element(`#\31  > div.c-abstract`).Text() //这里的选择器selector是从chrome控制台中copy的。

    fmt.Println(text)

    fmt.Println("done")

    // Skip
    // Output: done
}
func main() {

    Example_reuse_sessions()
    for {
    }

}

比较简洁、直观。

相关推荐