Skip to content

GoScrapy: Harnessing Go's power for blazingly fast web scraping, inspired by Python's Scrapy framework.

License

Notifications You must be signed in to change notification settings

tech-engine/goscrapy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoScrapy: Web Scraping Framework in Go

Alt Text

GoScrapy aims to be a powerful web scraping framework in Go, inspired by Python's Scrapy framework. It offers an easy-to-use Scrapy-like experience for extracting data from websites, making it an ideal tool for various data collection and analysis tasks, especially for those coming from Python and wanting to try scraping in Golang..

Getting Started

Goscrapy requires Go version 1.22 or higher to run.

1: Project Initialization

go mod init books_to_scrape

2. Install goscrapy cli

go install github.com/tech-engine/goscrapy@latest

Note: make sure to always keep your goscrapy cli updated.

3. Verify Installation

goscrapy -v

4. Create a New Project

goscrapy startproject books_to_scrape

This will create a new project directory with all the files necessary to begin working with GoScrapy.

\iyuioy\go\go-test-scrapy> goscrapy startproject books_to_scrape

🚀 GoScrapy generating project files. Please wait!

✔️  books_to_scrape\constants.go
✔️  books_to_scrape\errors.go
✔️  books_to_scrape\job.go
✔️  main.go
✔️  books_to_scrape\record.go
✔️  books_to_scrape\spider.go

✨ Congrates. books_to_scrape created successfully.

spider.go

In your spider.go file, set up and execute your spider.

For detailed code, please refer to the sample code here.

package scrapejsp

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"github.com/tech-engine/goscrapy/cmd/gos"
	"github.com/tech-engine/goscrapy/pkg/core"
)

type Spider struct {
	gos.ICoreSpider[*Record]
}

func NewSpider(ctx context.Context) (*Spider, <-chan error) {

	// use proxies
	// proxies := core.WithProxies("proxy_url1", "proxy_url2", ...)
	// core := gos.New[*Record]().WithClient(
	// 	gos.DefaultClient(proxies),
	// )

	core := gos.New[*Record]()

	// Add middlewares
	core.MiddlewareManager.Add(MIDDLEWARES...)
	// Add pipelines
	core.PipelineManager.Add(PIPELINES...)

	errCh := make(chan error)

	go func() {
		errCh <- core.Start(ctx)
	}()

	return &Spider{
		core,
	}, errCh
}

// This is the entrypoint to the spider
func (s *Spider) StartRequest(ctx context.Context, job *Job) {

	req := s.NewRequest()
	// req.Meta("JOB", job)
	req.Url("https://jsonplaceholder.typicode.com/todos/1")

	s.Request(req, s.parse)
}

func (s *Spider) Close(ctx context.Context) {
}

func (s *Spider) parse(ctx context.Context, resp core.IResponseReader) {
	fmt.Printf("status: %d", resp.StatusCode())

	var data Record
	err := json.Unmarshal(resp.Bytes(), &data)
	if err != nil {
		log.Fatalln(err)
	}

	// to push to pipelines
	s.Yield(&data)
}

Wiki

Please follow the wiki docs for details.

Note

GoScrapy is not stable, so its API may change drastically. Please exercise caution when using it in production.

License

GoScrapy is available under the BSL with an additional usage grant that allows free internal use. Please ensure that you agree with the license before contributing to GoScrapy, as by contributing to the GoScrapy project, you agree to the terms of the license.

Roadmap

  • Cookie management
  • Builtin & Custom Middlewares support
  • Css & Xpath Selectors
  • Logging
  • Triggers
  • Tests(work in progress)

Partners

Get in touch

Discord

About

GoScrapy: Harnessing Go's power for blazingly fast web scraping, inspired by Python's Scrapy framework.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages