Skip to content

Commit 4bc6a18

Browse files
add examples
1 parent 6882cde commit 4bc6a18

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
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

1515
Made by front-ender for front-enders.
1616
Package 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.
1717
It's only for reading DOM, searching elements and getting their data.
1818
Doesn'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

examples/stackOverflowPosts.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

0 commit comments

Comments
 (0)