用rod获取百度搜索结果的例子
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 { } }
比较简洁、直观。
相关推荐
rojyang 2020-05-20
baynkbtg 2019-11-02
ksjlhy 2019-10-28
geminihr 2015-03-19
chang00 2015-06-17
LinSeeker 2019-09-07
nextwhy 2012-09-09
tongjianru 2011-05-18
Jarvan 2016-09-25
fadacai0 2016-04-18
deusrwvf 2017-06-21
AIOps智能运维 2015-05-25
magvwiz 2013-10-18
AIOps智能运维 2010-08-17
linqiang 2010-02-04
XSxing 2019-06-30
XSxing 2019-06-30