MCPcopy Create free account
hub / github.com/Stylsheets/TaskEaseGPT / Scrape

Function Scrape

golang/internal/scraper/scraper.go:21–57  ·  view source on GitHub ↗
(url string)

Source from the content-addressed store, hash-verified

19}
20
21func Scrape(url string) (Result, error) {
22 c := colly.NewCollector()
23
24 result := Result{
25 Titles: make(map[string][]string),
26 }
27
28 c.OnHTML("a[href]", func(e *colly.HTMLElement) {
29 link := e.Attr("href")
30 name := strings.TrimSpace(e.Text)
31 result.Links = append(result.Links, Link{Name: name, URL: link})
32 })
33
34 c.OnHTML("h1, h2, h3", func(e *colly.HTMLElement) {
35 tag := e.Name
36 result.Titles[tag] = append(result.Titles[tag], e.Text)
37 })
38
39 c.OnHTML("body", func(e *colly.HTMLElement) {
40 result.TextContent = strings.TrimSpace(e.Text)
41
42 // text has parts like \n\n\n... or \t\n\t\n... clean duplicates and make them single \n
43 // Compile the regular expression
44 regex := regexp.MustCompile(`((\n+)|(\t+))`)
45 result.TextContent = regex.ReplaceAllString(result.TextContent, "\n")
46 regex = regexp.MustCompile(`\n+`)
47 result.TextContent = regex.ReplaceAllString(result.TextContent, "\n")
48 fmt.Println(result.TextContent)
49 })
50
51 err := c.Visit(url)
52 if err != nil {
53 return Result{}, err
54 }
55
56 return result, nil
57}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected