Instagram
youtube
Facebook

Web scrapping with golang and colly

Learn how to create a web scraper in Go using the Colly library. With Colly, you can easily extract data from websites and web pages using simple and efficient Go code. In this tutorial, we'll walk you through the steps of setting up a Colly collector, defining HTML callbacks, and visiting a website to start scraping. We'll also show you how to customize and extend your web scraper to fit your specific needs, such as handling cookies, following links, and storing data in a database. By the end of this tutorial, you'll have a fully-functional web scraper built with Golang and Colly.

What is golang colly

Colly is a popular open-source library for web scraping and crawling written in Go (also known as Golang). It provides a simple and flexible API for building web scrapers and crawlers that can extract data from websites and web pages.

Colly is designed to be easy to use and efficient, with a focus on concurrency and performance. It provides a number of features that make it well-suited for web scraping and crawling, including support for a wide range of protocols (e.g. HTTP, HTTPS, FTP), the ability to follow links, support for custom request headers and cookies, and the ability to use different storage backends (e.g. in-memory, Redis, MongoDB).

Colly is widely used by developers to build web scrapers and crawlers for a variety of purposes, such as data mining, price comparison, and search engine indexing. It is a powerful tool for extracting data from the web and can be used to build a wide range of applications and services.
 

Here is a simple example of how you can use the Colly library to scrape a website in Go:

package main

import (
	"fmt"

	"github.com/gocolly/colly"
)

func main() {
	// Create a new colly collector
	c := colly.NewCollector()

	// Set HTML callback
	c.OnHTML("a[href]", func(e *colly.HTMLElement) {
		// Print link
		fmt.Println(e.Attr("href"))
	})

	// Start scraping on https://www.example.com
	c.Visit("https://www.example.com")
}

This code creates a new Colly collector and sets an HTML callback function that will be called for each a element with an href attribute found in the HTML document. The callback function prints the value of the href attribute to the console.

To start the scraping, the code calls the Visit method with the URL of the website to be scraped (https://www.example.com in this case). Colly will then send an HTTP request to this URL, download the HTML document, and extract all the links from the document.

This is a very basic example of web scraping with Colly and Go, and you can customize and extend it to fit your specific needs. For example, you can add more callbacks to extract other types of data, or use Colly's advanced features to handle cookies, follow links, or store data in a database.
 

How to store data to db using colly golang

To store data to a database using Colly in Go, you can use one of Colly's storage backends or write your own custom storage backend. Colly provides built-in support for storing data in memory, Redis, and MongoDB, and you can use these backends by specifying the appropriate options when creating a Colly collector.

Here is an example of how you can use the in-memory storage backend to store data in Colly:
 

package main

import (
	"fmt"

	"github.com/gocolly/colly"
)

func main() {
	// Create a new colly collector
	c := colly.NewCollector(
		colly.MaxDepth(1),
		colly.Async(true),
		colly.Debugger(&debug.LogDebugger{}),
	)

	// Set HTML callback
	c.OnHTML("a[href]", func(e *colly.HTMLElement) {
		// Save link to the in-memory storage backend
		c.Visit(e.Request.AbsoluteURL(e.Attr("href")))
	})

	// Set request callback
	c.OnRequest(func(r *colly.Request) {
		// Print request information
		fmt.Println("Visiting", r.URL)
	})

	// Start scraping on https://www.example.com
	c.Visit("https://www.example.com")

	// Wait until all scrapes are finished
	c.Wait()

	// Print the stored data
	fmt.Println("Stored")
}

 

Add a comment:

Comments:

ylxfmdsfnf at Feb. 2, 2023, 6:04 p.m.
Web scrapping with golang and colly <a href="http://www.ge7hws59nxm30s391zi5o1j07k9c742fs.org/">aylxfmdsfnf</a> ylxfmdsfnf http://www.ge7hws59nxm30s391zi5o1j07k9c742fs.org/ [url=http://www.ge7hws59nxm30s391zi5o1j07k9c742fs.org/]uylxfmdsfnf[/url]
wfsiesttm at Feb. 2, 2023, 3:02 p.m.
Web scrapping with golang and colly <a href="http://www.gb467r1i505wuv7fucab891k11z810fgs.org/">awfsiesttm</a> wfsiesttm http://www.gb467r1i505wuv7fucab891k11z810fgs.org/ [url=http://www.gb467r1i505wuv7fucab891k11z810fgs.org/]uwfsiesttm[/url]
qktjqpldc at Jan. 30, 2023, 9:38 p.m.
Web scrapping with golang and colly <a href="http://www.gfb623xan054550a365s7bjjvm44e2ghs.org/">aqktjqpldc</a> qktjqpldc http://www.gfb623xan054550a365s7bjjvm44e2ghs.org/ [url=http://www.gfb623xan054550a365s7bjjvm44e2ghs.org/]uqktjqpldc[/url]
elvgdhleok at Jan. 30, 2023, 6:49 a.m.
Web scrapping with golang and colly elvgdhleok http://www.gu3g1m44564m7dz369iu20tnjet4e72es.org/ <a href="http://www.gu3g1m44564m7dz369iu20tnjet4e72es.org/">aelvgdhleok</a> [url=http://www.gu3g1m44564m7dz369iu20tnjet4e72es.org/]uelvgdhleok[/url]
demqeezhc at Jan. 29, 2023, 7:56 p.m.
Web scrapping with golang and colly demqeezhc http://www.ghia3hly81906uw6m7gb6395903mxj5qs.org/ [url=http://www.ghia3hly81906uw6m7gb6395903mxj5qs.org/]udemqeezhc[/url] <a href="http://www.ghia3hly81906uw6m7gb6395903mxj5qs.org/">ademqeezhc</a>
xfyyypbekc at Jan. 28, 2023, 2:36 p.m.
Web scrapping with golang and colly xfyyypbekc http://www.g047ua0lr9frrh17f64285o3e1z08srns.org/ <a href="http://www.g047ua0lr9frrh17f64285o3e1z08srns.org/">axfyyypbekc</a> [url=http://www.g047ua0lr9frrh17f64285o3e1z08srns.org/]uxfyyypbekc[/url]
osowmtijb at Jan. 25, 2023, 5:58 p.m.
Web scrapping with golang and colly [url=http://www.g9554je5ee6i9x5sf41r0d8bg245s5oas.org/]uosowmtijb[/url] <a href="http://www.g9554je5ee6i9x5sf41r0d8bg245s5oas.org/">aosowmtijb</a> osowmtijb http://www.g9554je5ee6i9x5sf41r0d8bg245s5oas.org/
lfhhntqoq at Jan. 21, 2023, 8:09 a.m.
Web scrapping with golang and colly <a href="http://www.g2qv7zp7oo3u1sde9494n543h4b958uhs.org/">alfhhntqoq</a> [url=http://www.g2qv7zp7oo3u1sde9494n543h4b958uhs.org/]ulfhhntqoq[/url] lfhhntqoq http://www.g2qv7zp7oo3u1sde9494n543h4b958uhs.org/
bkqmzhkfo at Jan. 20, 2023, 7:50 a.m.
Web scrapping with golang and colly [url=http://www.g178g8iftu39n8t2z9rn7l70r80g7ih2s.org/]ubkqmzhkfo[/url] <a href="http://www.g178g8iftu39n8t2z9rn7l70r80g7ih2s.org/">abkqmzhkfo</a> bkqmzhkfo http://www.g178g8iftu39n8t2z9rn7l70r80g7ih2s.org/
volewsfks at Jan. 20, 2023, 2:20 a.m.
Web scrapping with golang and colly volewsfks http://www.gpm2p141h363yvo5tm03b7s0y47h1ow8s.org/ [url=http://www.gpm2p141h363yvo5tm03b7s0y47h1ow8s.org/]uvolewsfks[/url] <a href="http://www.gpm2p141h363yvo5tm03b7s0y47h1ow8s.org/">avolewsfks</a>