get with cookies
This commit is contained in:
parent
f9082aa509
commit
17946611f2
@ -2,6 +2,7 @@ package lwb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/cookiejar"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -9,25 +10,32 @@ import (
|
|||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type Browser struct {
|
||||||
Browser struct {
|
|
||||||
url string
|
url string
|
||||||
userAgent string
|
userAgent string
|
||||||
|
http *http.Client
|
||||||
document *html.Node
|
document *html.Node
|
||||||
|
cookies *cookiejar.Jar
|
||||||
mut sync.RWMutex
|
mut sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
Element struct {
|
func NewBrowser(userAgent string) (*Browser, error) {
|
||||||
node *html.Node
|
cj, err := cookiejar.New(nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hc := &http.Client{
|
||||||
|
Jar: cj,
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
func NewBrowser(userAgent string) *Browser {
|
|
||||||
b := &Browser{
|
b := &Browser{
|
||||||
userAgent: userAgent,
|
userAgent: userAgent,
|
||||||
|
cookies: cj,
|
||||||
|
http: hc,
|
||||||
}
|
}
|
||||||
|
|
||||||
return b
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Browser) Get(url string) error {
|
func (b *Browser) Get(url string) error {
|
||||||
@ -36,13 +44,14 @@ func (b *Browser) Get(url string) error {
|
|||||||
|
|
||||||
b.url = url
|
b.url = url
|
||||||
|
|
||||||
rest, err := http.Get(url)
|
resp, err := b.http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer rest.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
b.document, err = html.Parse(rest.Body)
|
// Parse the HTML document
|
||||||
|
b.document, err = html.Parse(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
32
element.go
Normal file
32
element.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package lwb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/net/html"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Element struct {
|
||||||
|
node *html.Node
|
||||||
|
browser *Browser
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrorNotClickable = errors.New("element is not clickable")
|
||||||
|
|
||||||
|
func (self *Element) Click() error {
|
||||||
|
for e := self.node; e != nil; e = e.Parent {
|
||||||
|
if e.Type != html.ElementNode {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.ToLower(e.Data) == "a" {
|
||||||
|
for _, a := range e.Attr {
|
||||||
|
if a.Key == "href" {
|
||||||
|
return self.browser.Get(a.Val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ErrorNotClickable
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user