Created
April 10, 2025 10:54
-
-
Save philippta/d869a691099c90f14a570f536fc79a5f to your computer and use it in GitHub Desktop.
Flyscrape Library Usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment