File tree Expand file tree Collapse file tree 2 files changed +49
-1
lines changed
Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change 99<p align =" center " >
1010 <a href="https://pkg.go.dev/github.com/bringmetheaugust/goDOM"><img src="https://pkg.go.dev/badge/github.com/stretchr/testify" alt="Doc reference"></a>
1111 <a href="https://lh3.googleusercontent.com/proxy/w2a-pc4X9z2kuDWoXKnSF8pY6ngZvjVuZOAXMz3ZR8NwaUj9a-KsJnpcjtUSRO9QtFV6vMb3YoHWWv6k43Cb6bHOJEka19uE54GWtVx7Lru8gi10I_968eA2thkA0dL1O-zA8WT24cI"><img src="https://img.shields.io/badge/go%20version-1.21.5-61CFDD.svg?style=flat-square" alt="Golang version"></a>
12- <a href="https://cs4.pikabu.ru/post_img/big/2014/12/15/4/1418619408_1209550583.jpg"><img src="https://img.shields.io/badge/version-0.1.9 -blue" alt="project version"></a>
12+ <a href="https://cs4.pikabu.ru/post_img/big/2014/12/15/4/1418619408_1209550583.jpg"><img src="https://img.shields.io/badge/version-0.2.1 -blue" alt="project version"></a>
1313</p >
1414
1515Made by front-ender for front-enders.
1616Package provide method to parse HTML and get browser-like [ DOM] ( https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction#what_is_the_dom ) and DOM API.
1717It's only for reading DOM, searching elements and getting their data.
1818Doesn't have methods to mutate DOM.
1919
20+ ⚠️Before using it You should remember that sites can detect You as a bot and return unexpected HTML response.
21+
2022## Installation
2123
2224 go get github.com/bringmetheaugust/goDOM
@@ -55,6 +57,8 @@ func main() {
5557 }
5658` ` `
5759
60+ #### More real examples [here](./examples/).
61+
5862## Docs
5963
6064### Document
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "io"
6+ "net/http"
7+
8+ goDom "github.com/bringmetheaugust/goDOM"
9+ )
10+
11+ func main () {
12+ r , err := http .Get ("https://stackoverflow.com" )
13+
14+ if err != nil || r .StatusCode >= 400 {
15+ fmt .Printf ("Cann't get site: %d" , err )
16+ return
17+ }
18+
19+ defer r .Body .Close ()
20+
21+ bytes , _ := io .ReadAll (r .Body )
22+ document , _ := goDom .Create (bytes ) // create document
23+
24+ // find DOM elements as posts (.s-post-summary) and get their link elements (.s-post-summary--content-title a)
25+ posts , err := document .QuerySelectorAll (".s-post-summary .s-post-summary--content-title a" )
26+
27+ // ! Remember that sites can detect You as a bot and return unexpected HTML response
28+ if err != nil {
29+ fmt .Println ("Ooops, posts not found." )
30+ return
31+ }
32+
33+ // loop link elements (HTMLAnchorElement)
34+ for _ , c := range posts {
35+ attr , err := c .GetAttribute ("href" ) // get href attribute
36+
37+ if err != nil {
38+ fmt .Println ("Ooops, href not found." )
39+ return
40+ }
41+
42+ fmt .Println (attr )
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments