Go教程

go rod

本文主要是介绍go rod,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

...

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
)

type TbPars struct {
	NavUrl    string // first url requst
	PreElm    string // xpath to loc pre elm
	DataSel   string
	Ln, Col   int
	PageCount int
	Next      string
}

func (t *TbPars) Pars() {
	path, _ := launcher.LookPath()
	u := launcher.New().Delete("--headless").Set("--window-size=1920,1080").Bin(path).MustLaunch()
	brs := rod.New().ControlURL(u).MustConnect()
	page := brs.MustPage(t.NavUrl).MustWaitLoad()
	log.Println("flag1====")
	page.Mouse.MustScroll(1600, 1600)
	page.MustElement(t.PreElm).MustClick()
	log.Println("flag====")
	// page.Keyboard.MustPress(35)

	elm := page.MustElementX(`/html/body/div[1]/table/tbody/tr[7]/td[1]/div`)
	text := elm.MustText()
	log.Println(text)
	for i := 1; i <= t.Ln; i++ {
		ln := []string{}
		for j := 1; j <= t.Col; j++ {
			sel := fmt.Sprintf(t.DataSel, i, j)
			// log.Println(i, j, sel)
			text := page.MustElementX(sel).MustText()
			log.Println("get ===>", text)
			ln = append(ln, text)
		}
		log.Println(ln)
	}
	time.Sleep(30 * time.Second)

}

func Foo() {

	t := TbPars{
		NavUrl: "https://www.eiguleo.com",
		// PreElm: "#tab-de",
		PreElm:  "#tab-eigu",
		DataSel: "/html/body/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[%d]/td[%d]/div",
		Ln:      10, Col: 9,
		PageCount: 18, Next: "#eigu-app > div.content",
	}
	t.Pars()

}

func main() {
	Foo()
}

  

这篇关于go rod的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!