You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
17
-
It's only for reading DOM, searching elements and getting their data.
18
-
Doesn't have methods to mutate DOM.
16
+
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.
17
+
It also has [jQuery](https://jquery.com/)-like API.
19
18
19
+
⚠️It's only for reading DOM, searching elements and getting their data.
20
+
Package doesn't have methods to mutate DOM.
20
21
⚠️Before using it You should remember that sites can detect You as a bot and return unexpected HTML response.
21
22
22
23
## Installation
@@ -25,39 +26,57 @@ Doesn't have methods to mutate DOM.
25
26
26
27
## Examples
27
28
28
-
* First, we'll get document
29
+
#### Using DOM-like API
29
30
30
31
```go
31
32
package motherfckrs
32
33
33
34
import"github.com/bringmetheaugust/goDOM"
34
35
35
36
funcmain() {
37
+
// First, we'll get document
36
38
bytes:=// HTML markup as bytes (from HTTP request, files, etc.)
37
-
document, domErr:= goDom.Create(bytes) // create document (DOM with DOM API, like in browser)
38
-
if domErr != nil {return} // also check if markup is valid
39
+
document, _, err:= goDom.Create(bytes) // create document (DOM with DOM API, like in browser)
40
+
if err != nil {return} // also check if markup is valid
41
+
42
+
// Want to find some element by `id`?
43
+
el, err:= document.GetElementById("lol") // <a id="lol" class="pipi" href="http://lol.com">
This package uses jQuery API as the origin jQuery library (using JavaScript).
141
+
For example in Golang with best practice, we should return data and/or errors almost from each function. How it should look like:
142
+
143
+
```go
144
+
a, err:=jQ(".li") // get elements with `li` classes
145
+
if err != nil { return } // check if elements exist
146
+
b, err:= a.Has("a.my-link") // filter if they have links with class `my-link` inside itself
147
+
if err != nil { return } // check if elements exist
148
+
c, err:= b.Find("div[data-lol=lala]") // find `div` elements with attribute `data-lol=lala` inside
149
+
if err != nil {return} // check if elements exist
150
+
c.Each(func (q) { print(q) })
151
+
```
152
+
153
+
As You know, this package provides the original jQuery API, so every method (except `Attr`) always returns another jQuery element, even if elements are not found and a slice of elements is empty. So we can use jQuery as in origin jQuery library like this:
0 commit comments