Shifter verwenden mit Go
Binden Sie Shifters Residential- und ISP-Proxys in wenigen Minuten in Go ein. Funktioniert mit dem Standard-net/http, Resty, GoQuery, Colly und jedem Client, der eine *url.URL verarbeitet – kein SDK erforderlich.
Schnellstart
Installieren
go get github.com/go-resty/resty/v2 Grundlegende Nutzung
package main
import (
"fmt"
"io"
"net/http"
"net/url"
)
func main() {
proxyURL, _ := url.Parse(
"customer-USERNAME-country-us-sid-123ABC:PASSWORD@p.shifter.io:443",
)
client := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
}
resp, _ := client.Get("https://ipinfo.io/json")
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
// {"ip": "154.16.xxx.xxx", "city": "New York", "country": "US", ...}
} Funktionen
Beispiele
Standard net/http mit Sticky Session
Die Option ohne Abhängigkeiten. Erstellen Sie einen *http.Transport mit einer Proxy-URL – derselbe Client kann sicher über Goroutinen hinweg wiederverwendet werden.
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
func newClient(country, city string) *http.Client {
sidBytes := make([]byte, 4)
rand.Read(sidBytes)
sid := hex.EncodeToString(sidBytes)
proxyURL, _ := url.Parse(fmt.Sprintf(
"customer-USERNAME-country-%s-city-%s-sid-%s-ttl-300:PASSWORD@p.shifter.io:443",
country, city, sid,
))
return &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
MaxIdleConnsPerHost: 16,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 20 * time.Second,
},
}
}
func main() {
c := newClient("uk", "london")
for _, path := range []string{"/login", "/dashboard", "/orders"} {
req, _ := http.NewRequest("GET", "https://example.co.uk"+path, nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
resp, err := c.Do(req)
if err != nil {
fmt.Println("error:", err)
continue
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
fmt.Println(path, resp.StatusCode, len(body), "bytes")
}
} Resty (ergonomischer)
Resty umhüllt net/http mit einem fluenten Builder, automatischen Wiederholungsversuchen und Response-Binding. Empfehlenswert für Scraping-Pipelines, die eine saubere Fehlerbehandlung benötigen.
package main
import (
"fmt"
"github.com/go-resty/resty/v2"
)
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
func main() {
client := resty.New().
SetProxy("customer-USERNAME-country-de-sid-456DEF:PASSWORD@p.shifter.io:443").
SetRetryCount(3).
SetRetryWaitTime(2_000_000_000). // 2s in ns
SetHeader("User-Agent", "Mozilla/5.0 (Macintosh) AppleWebKit/537.36")
var products []Product
resp, err := client.R().
SetResult(&products).
Get("https://api.example.de/products")
if err != nil || !resp.IsSuccess() {
fmt.Println("error:", err, resp.Status())
return
}
for _, p := range products {
fmt.Printf("%d %s — %.2f EUR\n", p.ID, p.Name, p.Price)
}
} Colly (Scraping-Framework)
Colly ist die De-facto-Go-Scraping-Bibliothek. Konfigurieren Sie den Proxy mit SetProxy() – jede Anfrage des Spiders wird über Shifter geleitet.
package main
import (
"fmt"
"log"
"github.com/gocolly/colly/v2"
)
func main() {
c := colly.NewCollector(
colly.AllowedDomains("example.com"),
colly.UserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"),
)
if err := c.SetProxy(
"customer-USERNAME-country-fr-city-paris-sid-789GHI:PASSWORD@p.shifter.io:443",
); err != nil {
log.Fatal(err)
}
c.OnHTML("article.post", func(e *colly.HTMLElement) {
fmt.Println(e.ChildText("h2"), "->", e.ChildAttr("a", "href"))
})
c.OnError(func(r *colly.Response, err error) {
fmt.Printf("error %d: %v\n", r.StatusCode, err)
})
c.Visit("https://example.com/blog")
} chromedp (Headless Chromium)
Steuern Sie eine echte Chromium-Instanz über Shifter für JavaScript-gerenderte Ziele. chromedp verwendet Chromes CDP-Protokoll – keine Selenium-Abhängigkeit.
package main
import (
"context"
"fmt"
"time"
"github.com/chromedp/chromedp"
)
func main() {
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.ProxyServer("http://p.shifter.io:443"),
chromedp.Flag("headless", true),
)
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
var html string
err := chromedp.Run(ctx,
// chromedp doesn't natively prompt for proxy auth — use a Chrome
// extension or run a sidecar that injects credentials, or use
// Shifter's IP-whitelist auth instead of user/pass.
chromedp.Navigate("https://example.com"),
chromedp.WaitVisible("body", chromedp.ByQuery),
chromedp.OuterHTML("html", &html),
)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(len(html), "bytes")
} Häufig gefragt FAQ-Fragen
Häufige Fragen zur Verwendung von Shifter mit Go.
Analysiere die Proxy-URL mit url.Parse und übergib sie dann über http.ProxyURL(proxyURL) an ein *http.Transport. Verwende diesen Transport für einen *http.Client. Der Client kann sicher über Goroutines hinweg geteilt werden und verwendet Verbindungen automatisch wieder.
Shifter verwenden mit Go
Füge Shifters 205M+ Residential- und ISP-Proxys in unter 5 Minuten zu deinen Go-Diensten hinzu. Kompatibel mit net/http, Resty, Colly und chromedp.