Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go + Colly/v2 #621

Open
coloraven opened this issue Jan 9, 2024 · 0 comments
Open

Go + Colly/v2 #621

coloraven opened this issue Jan 9, 2024 · 0 comments
Labels
requested generator a request to generate code in a new language/library

Comments

@coloraven
Copy link

Give the curl command
And output as blow:

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/gocolly/colly/v2"
	"github.com/gocolly/colly/v2/debug"
)

// creat a new collector
var c = colly.NewCollector(
	colly.Async(true), // Enable Async
	colly.AllowURLRevisit(),
	colly.Debugger(&debug.LogDebugger{}),
)

func init() {
	// The headers
	Headers := map[string]string{
		"User-Agent":    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
		"Accept-Custom": "en-US,en;q=0.9",
	}

	// Set proxy
	err := c.SetProxy("http://127.0.0.1:xxx")
	if err != nil {
		log.Fatal("set proxy failed:", err)
	}

	c.Limit(&colly.LimitRule{
		DomainGlob:  "*",
		Parallelism: 5,
		RandomDelay: 5 * time.Second,
	})

	c.OnRequest(func(r *colly.Request) {
		if r.Method == "GET" {
		} else if r.Method == "POST" {
		}

		// Set headers
		for k, v := range Headers {
			r.Headers.Set(k, v)
		}
	})

}
func main() {
	url := "https://httpbin.org/html"
	c.OnResponse(func(r *colly.Response) {
		fmt.Println("Response Text:", string(r.Body))
	})
	err := c.Visit(url)
	if err != nil {
		fmt.Println("Visit error:", err)
	}

	c.Wait()
}
@verhovsky verhovsky changed the title Hope to support Golang Colly/v2 Go + Colly/v2 Jan 9, 2024
@verhovsky verhovsky added the requested generator a request to generate code in a new language/library label Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
requested generator a request to generate code in a new language/library
Development

No branches or pull requests

2 participants