diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2025-04-10 12:53:29 +0200 |
|---|---|---|
| committer | Philipp Tanlak <philipp.tanlak@gmail.com> | 2025-04-10 12:53:29 +0200 |
| commit | b15b921f633730cb88169deb787bfe893cd40aae (patch) | |
| tree | 3da9ac1bb9419399d98af5bebd3b1022421aca67 /examples | |
| parent | bf99c233a18c3165e0d4d251b41224e5bc6eb93d (diff) | |
Support library usagegolib
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/flyscrapelib.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/flyscrapelib.go b/examples/flyscrapelib.go new file mode 100644 index 0000000..641e7ac --- /dev/null +++ b/examples/flyscrapelib.go @@ -0,0 +1,41 @@ +package main + +import "github.com/philippta/flyscrape/flyscrape" + +var config = flyscrape.Config{ + URL: "https://news.ycombinator.com/", + Browser: true, + Headless: false, +} + +func scrape(ctx flyscrape.Context) any { + var ( + post = ctx.Doc.Find(".athing.submission").First() + title = post.Find(".titleline > a").Text() + commentsLink = post.Next().Find("a").Last().Attr("href") + comments = ctx.Scrape(commentsLink, scrapeComments) + ) + + return flyscrape.M{ + "title": title, + "comments": comments, + } +} + +func scrapeComments(ctx flyscrape.Context) any { + var comments []flyscrape.M + + for _, comment := range ctx.Doc.Find(".comtr").All() { + comments = append(comments, flyscrape.M{ + "author": comment.Find(".hnuser").Text(), + "text": comment.Find(".commtext").Text(), + }) + + } + + return comments +} + +func main() { + flyscrape.Run(config, scrape) +} |