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
goquery brings a syntax and a set of features similar to [jQuery][] to the [Go language][go]. It is based on Go's [net/html package][html] and the CSS Selector library [cascadia][]. Since the net/html parser returns nodes, and not a full-featured DOM tree, jQuery's stateful manipulation functions (like height(), css(), detach()) have been left off.
5
8
@@ -19,7 +22,7 @@ Syntax-wise, it is as close as possible to jQuery, with the same function names
19
22
20
23
## Installation
21
24
22
-
Please note that because of the net/html dependency, goquery requires Go1.1+.
25
+
Please note that because of the net/html dependency, goquery requires Go1.1+ and is tested on Go1.7+.
23
26
24
27
$ go get github.com/PuerkitoBio/goquery
25
28
@@ -37,6 +40,9 @@ Please note that because of the net/html dependency, goquery requires Go1.1+.
37
40
38
41
**Note that goquery's API is now stable, and will not break.**
39
42
43
+
***2021-10-25 (v1.8.0)** : Add `Render` function to render a `Selection` to an `io.Writer` (thanks [@anthonygedeon](https://github.com/anthonygedeon)).
***2021-06-14 (v1.7.0)** : Add `Single` and `SingleMatcher` functions to optimize first-match selection (thanks [@gdollardollar](https://github.com/gdollardollar)).
40
46
***2021-01-11 (v1.6.1)** : Fix panic when calling `{Prepend,Append,Set}Html` on a `Selection` that contains non-Element nodes.
41
47
***2020-10-08 (v1.6.0)** : Parse html in context of the container node for all functions that deal with html strings (`AfterHtml`, `AppendHtml`, etc.). Thanks to [@thiemok][thiemok] and [@davidjwilkins][djw] for their work on this.
@@ -50,7 +56,7 @@ Please note that because of the net/html dependency, goquery requires Go1.1+.
50
56
***2016-08-28 (v1.0.1)** : Optimize performance for large documents.
51
57
***2016-07-27 (v1.0.0)** : Tag version 1.0.0.
52
58
***2016-06-15** : Invalid selector strings internally compile to a `Matcher` implementation that never matches any node (instead of a panic). So for example, `doc.Find("~")` returns an empty `*Selection` object.
53
-
***2016-02-02** : Add `NodeName` utility function similar to the DOM's `nodeName` property. It returns the tag name of the first element in a selection, and other relevant values of non-element nodes (see godoc for details). Add `OuterHtml` utility function similar to the DOM's `outerHTML` property (named `OuterHtml` in small caps for consistency with the existing `Html` method on the `Selection`).
59
+
***2016-02-02** : Add `NodeName` utility function similar to the DOM's `nodeName` property. It returns the tag name of the first element in a selection, and other relevant values of non-element nodes (see [doc][] for details). Add `OuterHtml` utility function similar to the DOM's `outerHTML` property (named `OuterHtml` in small caps for consistency with the existing `Html` method on the `Selection`).
54
60
***2015-04-20** : Add `AttrOr` helper method to return the attribute's value or a default value if absent. Thanks to [piotrkowalczuk][piotr].
55
61
***2015-02-04** : Add more manipulation functions - Prepend* - thanks again to [Andrew Stone][thatguystone].
56
62
***2014-11-28** : Add more manipulation functions - ReplaceWith*, Wrap* and Unwrap - thanks again to [Andrew Stone][thatguystone].
@@ -79,7 +85,7 @@ jQuery often has many variants for the same function (no argument, a selector st
79
85
80
86
Utility functions that are not in jQuery but are useful in Go are implemented as functions (that take a `*Selection` as parameter), to avoid a potential naming clash on the `*Selection`'s methods (reserved for jQuery-equivalent behaviour).
81
87
82
-
The complete [godoc reference documentation can be found here][doc].
88
+
The complete [package reference documentation can be found here][doc].
83
89
84
90
Please note that Cascadia's selectors do not necessarily match all supported selectors of jQuery (Sizzle). See the [cascadia project][cascadia] for details. Invalid selector strings compile to a `Matcher` that fails to match any node. Behaviour of the various functions that take a selector string as argument follows from that fact, e.g. (where `~` is an invalid selector string):
85
91
@@ -123,12 +129,11 @@ func ExampleScrape() {
123
129
}
124
130
125
131
// Find the review items
126
-
doc.Find(".sidebar-reviews article .content-block").Each(func(i int, s *goquery.Selection) {
127
-
// For each item found, get the band and title
128
-
band:= s.Find("a").Text()
129
-
title:= s.Find("i").Text()
130
-
fmt.Printf("Review %d: %s - %s\n", i, band, title)
131
-
})
132
+
doc.Find(".left-content article .post-title").Each(func(i int, s *goquery.Selection) {
133
+
// For each item found, get the title
134
+
title:= s.Find("a").Text()
135
+
fmt.Printf("Review %d: %s\n", i, title)
136
+
})
132
137
}
133
138
134
139
funcmain() {
@@ -149,6 +154,8 @@ func main() {
149
154
-[Geziyor](https://github.com/geziyor/geziyor), a fast web crawling & scraping framework for Go. Supports JS rendering.
150
155
-[Pagser](https://github.com/foolin/pagser), a simple, easy, extensible, configurable HTML parser to struct based on goquery and struct tags.
151
156
-[stitcherd](https://github.com/vhodges/stitcherd), A server for doing server side includes using css selectors and DOM updates.
157
+
-[goskyr](https://github.com/jakopako/goskyr), an easily configurable command-line scraper written in Go.
158
+
-[goGetJS](https://github.com/davemolk/goGetJS), a tool for extracting, searching, and saving JavaScript files (with optional headless browser).
152
159
153
160
## Support
154
161
@@ -161,8 +168,9 @@ There are a number of ways you can support the project:
161
168
* Pull requests: please discuss new code in an issue first, unless the fix is really trivial.
162
169
- Make sure new code is tested.
163
170
- Be mindful of existing code - PRs that break existing code have a high probability of being declined, unless it fixes a serious issue.
164
-
165
-
If you desperately want to send money my way, I have a BuyMeACoffee.com page:
171
+
* Sponsor the developer
172
+
- See the Github Sponsor button at the top of the repo on github
173
+
- or via BuyMeACoffee.com, below
166
174
167
175
<ahref="https://www.buymeacoffee.com/mna"target="_blank"><imgsrc="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png"alt="Buy Me A Coffee"style="height: 41px!important;width: 174px!important;box-shadow: 0px3px2px0pxrgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px3px2px0pxrgba(190, 190, 190, 0.5) !important;" ></a>
168
176
@@ -177,10 +185,10 @@ The [BSD 3-Clause license][bsd], the same as the [Go language][golic]. Cascadia'
0 commit comments