Scraper avec Go

La librairie standard net/http suffit : pas de dépendance externe pour appeler WyndPath en Go.

Premier appel

package main

import (
    "fmt"
    "io"
    "net/http"
    "net/url"
)

func main() {
    q := url.Values{}
    q.Set("api_key", "VOTRE_CLE")
    q.Set("url", "https://exemple.com")

    resp, err := http.Get("https://api.wyndpath.com/v1/?" + q.Encode())
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    body, _ := io.ReadAll(resp.Body)
    fmt.Println(resp.StatusCode)
    fmt.Println(string(body))
}

Rendu JavaScript et pays

q := url.Values{}
q.Set("api_key", "VOTRE_CLE")
q.Set("url", "https://exemple.com")
q.Set("render_js", "1")
q.Set("country", "fr")
q.Set("max_cost", "15")

Lire les crédits

fmt.Println(resp.Header.Get("X-WyndPath-Credits"))

Requête POST

q := url.Values{}
q.Set("api_key", "VOTRE_CLE")
q.Set("url", "https://exemple.com/api")

body := strings.NewReader(`{"q":"pikachu"}`)
resp, _ := http.Post(
    "https://api.wyndpath.com/v1/?"+q.Encode(),
    "application/json",
    body,
)

Gérer les erreurs

if resp.StatusCode != http.StatusOK {
    b, _ := io.ReadAll(resp.Body)
    fmt.Printf("Erreur %d : %s\n", resp.StatusCode, b)
}
✓ Payé au succès. Un appel qui échoue ne consomme aucun crédit.