Skip to content
Projets

À propos

Golang Crawling and scraping framework

Données et analyse

Comment ces chiffres sont mesurés

What you can do

  • Low level API & Easy High Level API
  • Customizable retry and error handling
  • Javascript Rendering with ability to control the browser
  • Screenshots support (when JS rendering is enabled)

Progression mesurée des étoiles

in 24h
0
in 7 days
+1
in 15 days
+1
in 1 month
Historique en cours

Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.

README.md

Original

scrapemate

Documentation GoDoc build Go Report Card

Scrapemate is a web crawling and scraping framework written in Golang. It is designed to be simple and easy to use, yet powerful enough to handle complex scraping tasks.

Read a page as Markdown

With the CLI on your path, fetch a page directly. HTTP mode needs no browser or API key.

scrapemate fetch https://example.com --format markdown

To try it from this checkout, run go run ./cmd/scrapemate fetch https://example.com. For prebuilt binaries, JavaScript rendering, and the matching Go example, see the fetch guide. Coding agents can use the Scrapemate skill.

Featured sponsor

Webshare proxy offer — 20% off your first purchase with MAPSSCRAPER20

Scrapemate works with a direct connection. If you need a proxy, you can try Webshare through our sponsor referral link. Use MAPSSCRAPER20 for 20% off your first purchase.

Features

  • Low level API & Easy High Level API
  • Customizable retry and error handling
  • Javascript Rendering with ability to control the browser
  • Screenshots support (when JS rendering is enabled)
  • Capability to write your own result exporter
  • Capability to write results in multiple sinks
  • Default CSV writer
  • Caching (File/LevelDB/Custom)
  • Custom job providers (memory provider included)
  • Headless and Headful support when using JS rendering
  • Automatic cookie and session handling
  • Rotating HTTP/HTTPS/SOCKS5 proxy support

JavaScript Rendering

Scrapemate uses Playwright for JavaScript rendering. It requires the Playwright browsers to be installed.

# Install playwright browsers
go run github.com/mxschmitt/playwright-go/cmd/playwright install --with-deps chromium

Build and run without any special tags:

go build ./...

Example Usage

The books-to-scrape-simple example demonstrates JavaScript rendering with Playwright:

# Run with Playwright (default)
# First install browsers: go run github.com/mxschmitt/playwright-go/cmd/playwright install --with-deps chromium
go run . -js

Installation

go get github.com/gosom/scrapemate

Fetch a single page

Use github.com/gosom/scrapemate/scrape to fetch a URL and get the response directly.

client, err := scrape.New()
if err != nil {
    return err
}
defer client.Close()

page, err := client.Fetch(ctx, "https://example.com")
if err != nil {
    return err
}
fmt.Println(page.Response.StatusCode, page.Response.URL)
fmt.Println(page.HTML())

Reuse the client across calls to keep its cookie session. Pass scrape.WithProxies(proxies) to select a proxy for the lifetime of the client. HTTP error statuses such as 404 are available in page.Response along with the response body.

Run the single-page example from the repository root to fetch a page over HTTP.

go run ./examples/scrape-client https://example.com

For JavaScript pages, install Chromium and the matching Playwright driver, then use -js. The optional -wait flag tells the client to wait for a visible CSS selector before capturing the rendered HTML.

go run github.com/mxschmitt/playwright-go/cmd/playwright install --with-deps chromium
go run ./examples/scrape-client -js -wait 'h1' https://example.com

Set SCRAPEMATE_PROXY to a proxy URL to use it with either example command. See Use an authenticated proxy for an example and an outgoing-IP check.

Use HTTP mode to fetch attachments. If Chromium treats a response as a download, JavaScript mode returns a navigation error.

Job-based quickstart

package main

import (
	"context"
	"encoding/csv"
	"fmt"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/PuerkitoBio/goquery"
	"github.com/gosom/scrapemate"
	"github.com/gosom/scrapemate/adapters/writers/csvwriter"
	"github.com/gosom/scrapemate/scrapemateapp"
)

func main() {
	csvWriter := csvwriter.NewCsvWriter(csv.NewWriter(os.Stdout))

	cfg, err := scrapemateapp.NewConfig(
		[]scrapemate.ResultWriter{csvWriter},
	)
	if err != nil {
		panic(err)
	}
	app, err := scrapemateapp.NewScrapeMateApp(cfg)
	if err != nil {
		panic(err)
	}
	seedJobs := []scrapemate.IJob{
		&SimpleCountryJob{
			Job: scrapemate.Job{
				ID:     "identity",
				Method: http.MethodGet,
				URL:    "https://www.scrapethissite.com/pages/simple/",
				Headers: map[string]string{
					"User-Agent": scrapemate.DefaultUserAgent,
				},
				Timeout:    10 * time.Second,
				MaxRetries: 3,
			},
		},
	}
	err = app.Start(context.Background(), seedJobs...)
	if err != nil && err != scrapemate.ErrorExitSignal {
		panic(err)
	}
}

type SimpleCountryJob struct {
	scrapemate.Job
}

func (j *SimpleCountryJob) Process(ctx context.Context, resp *scrapemate.Response) (any, []scrapemate.IJob, error) {
	doc, ok := resp.Document.(*goquery.Document)
	if !ok {
		return nil, nil, fmt.Errorf("failed to cast response document to goquery document")
	}
	var countries []Country
	doc.Find("div.col-md-4.country").Each(func(i int, s *goquery.Selection) {
		var country Country
		country.Name = strings.TrimSpace(s.Find("h3.country-name").Text())
		country.Capital = strings.TrimSpace(s.Find("div.country-info span.country-capital").Text())
		country.Population = strings.TrimSpace(s.Find("div.country-info span.country-population").Text())
		country.Area = strings.TrimSpace(s.Find("div.country-info span.country-area").Text())
		countries = append(countries, country)
	})
	return countries, nil, nil
}

type Country struct {
	Name       string
	Capital    string
	Population string
	Area       string
}

func (c Country) CsvHeaders() []string {
	return []string{"Name", "Capital", "Population", "Area"}
}

func (c Country) CsvRow() []string {
	return []string{c.Name, c.Capital, c.Population, c.Area}
}
go mod tidy
go run main.go 1>countries.csv

(hit CTRL-C to exit)

Migrating from v0.9.x to v1.0.0

Version 1.0.0 introduces a BrowserPage interface abstraction for browser automation. This is a breaking change for users who use JavaScript rendering with BrowserActions.

Update BrowserActions signature

// Before (v0.9.x)
func (j *MyJob) BrowserActions(ctx context.Context, page playwright.Page) scrapemate.Response {
    page.Goto("https://example.com", playwright.PageGotoOptions{
        WaitUntil: playwright.WaitUntilStateNetworkidle,
    })
    html, _ := page.Content()
    return scrapemate.Response{Body: []byte(html)}
}

// After (v1.0.0)
func (j *MyJob) BrowserActions(ctx context.Context, page scrapemate.BrowserPage) scrapemate.Response {
    resp, err := page.Goto("https://example.com", scrapemate.WaitUntilNetworkIdle)
    if err != nil {
        return scrapemate.Response{Error: err}
    }
    return scrapemate.Response{
        Body:       resp.Body,
        StatusCode: resp.StatusCode,
    }
}

Accessing the underlying browser page

If you need browser-specific features, use Unwrap():

// For Playwright
pwPage := page.Unwrap().(playwright.Page)

See CHANGELOG.md for the full list of changes.

Documentation

You can find more documentation here

For the High Level API see this example.

Read also how to use high level api

For the Low Level API see books.toscrape.com

Additionally, for low level API you can read the blogpost

See an example of how you can use scrapemate go scrape Google Maps: https://github.com/gosom/google-maps-scraper

Contributing

Contributions are welcome.

Responsible use and sponsor disclaimer

Scrapemate is built for legitimate crawling and data collection. You are responsible for how you use it and for complying with applicable laws and the terms of the sites you access.

Sponsors operate their services independently. If you use a sponsor, you deal directly with that provider under its own terms. The maintainer disclaims responsibility for sponsors’ products, services, claims, or any losses arising from their use.

Licence

Scrapemate is licensed under the MIT License. See LICENCE file

Explorer des projets similaires

Chargement…