返回 Skill 列表
extension
分类: 数据与分析需要 API Key

Scrapfly SDK Go

Web scraping from Go with the Scrapfly SDK (`github.com/scrapfly/go-scrapfly`). Use when the user asks to scrape a URL, bypass anti-bot protection, render Ja...

person作者: scrapflyhubclawhub

Scrapfly Go SDK

Use the go-scrapfly module to scrape web pages from Go with proxy rotation, anti-bot bypass (ASP), JavaScript rendering, screenshots, and AI extraction. It ships a built-in goquery selector for parsing the response.

When to use

  • Scraping web pages (HTML, JSON, text, markdown) from a Go codebase
  • Bypassing anti-bot protections (Cloudflare, DataDome, PerimeterX, Kasada)
  • Rendering JavaScript-heavy pages with a headless browser
  • Capturing screenshots or extracting structured data

Setup

go get github.com/scrapfly/go-scrapfly
export SCRAPFLY_API_KEY="your-api-key"

The package name is scrapfly. Get your API key at scrapfly.io.

Basic scrape

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/scrapfly/go-scrapfly"
)

func main() {
    client, err := scrapfly.New(os.Getenv("SCRAPFLY_API_KEY"))
    if err != nil {
        log.Fatalf("failed to create client: %v", err)
    }

    apiResponse, err := client.Scrape(&scrapfly.ScrapeConfig{
        URL: "https://web-scraping.dev/product/1",
    })
    if err != nil {
        log.Fatalf("scrape failed: %v", err)
    }

    fmt.Println(apiResponse.Result.Content)
}

Anti-bot bypass, geo-targeting, JS rendering

ScrapeConfig fields are Go-idiomatic PascalCase. ProxyPool takes a typed constant.

apiResponse, err := client.Scrape(&scrapfly.ScrapeConfig{
    URL:             "https://web-scraping.dev/product/1",
    ASP:             true,                            // bypass anti-bot
    Country:         "us",                            // proxy country (ISO 3166-1 alpha-2)
    ProxyPool:       scrapfly.PublicResidentialPool,  // or scrapfly.PublicDataCenterPool
    RenderJS:        true,                            // headless browser rendering
    WaitForSelector: "div.product",                  // requires RenderJS
    RenderingWait:   5000,                            // ms, requires RenderJS
    Format:          "markdown",                      // raw | clean_html | json | markdown | text
})

Built-in HTML selector

selector, err := apiResponse.Selector() // goquery document
if err != nil {
    log.Fatal(err)
}
fmt.Println("Title:", selector.Find("h3").First().Text())

Screenshots and extraction

shot, err := client.Screenshot(&scrapfly.ScreenshotConfig{
    URL: "https://web-scraping.dev/products",
})

data, err := client.Extract(&scrapfly.ExtractionConfig{
    Body:             []byte(apiResponse.Result.Content),
    ContentType:      "text/html",
    ExtractionPrompt: "extract all product names and prices",
})

Important notes

  • RenderJS: true is required for RenderingWait, WaitForSelector, and screenshots
  • scrapfly.PublicResidentialPool is recommended alongside ASP: true
  • Use Format: "markdown" for clean, LLM-friendly content
  • Read the page body via apiResponse.Result.Content; parse with apiResponse.Selector()