Categorygithub.com/virrages/scrape
modulepackage
0.0.0-20160831163138-d912992925aa
Repository: https://github.com/virrages/scrape.git
Documentation: pkg.go.dev

# README

scrape

A jquery like interface for Go website scrapping.

Usage

import (
    "fmt"
    "net/http"

    "golang.org/x/net/html"

    "github.com/VirrageS/scrape"
)

func main() {
    response, err := http.Get("https://github.com/trending")
    if err != nil {
        return
    }

    root, err := html.Parse(response.Body)
    if err != nil {
        return
    }

    repos := scrape.Find(root, ".repo-list-item")
    for _, repo := range repos {
        // get url
        link := scrape.Find(repo, ".repo-list-name a")[0]
        url := "https://github.com" + scrape.Attr(link, "href")

        // get name
        name := scrape.Text(link)

        fmt.Printf("[REPO] name: %s; url: %s\n", name, url)
    }
}

# Functions

Attr returns the value of an HTML attribute.
Closest searches up HTML tree from the current node until either a match is found or the top is hit.
Find returns all nodes which match selector.
Text searches for text and concatenate all separated strings.