Golang Colly Web Scraping in Depth

Author

Reads 1.3K

Futuristic workspace featuring a glowing computer screen with coding displayed, ideal for technology and programming concepts.
Credit: pexels.com, Futuristic workspace featuring a glowing computer screen with coding displayed, ideal for technology and programming concepts.

Golang's Colly library is designed to make web scraping easier and more efficient. Colly uses a concurrent model to fetch pages and handle callbacks, making it well-suited for large-scale scraping tasks.

Colly's ability to handle callbacks allows for flexible and efficient data processing. This means you can write custom code to handle the data you scrape, making it perfect for a wide range of applications.

Colly's concurrent model is built on top of the standard library's goroutine and channel features. This allows for seamless integration with other Golang libraries and frameworks.

With Colly, you can easily handle common web scraping tasks, such as following links and submitting forms.

On a similar theme: Web Scraper Python Library

Installation and Setup

To get started with Go and Colly, you'll need to install the Colly library using the go get command. This will allow you to use Colly's web scraping features in your Golang projects.

To initiate a new Go project, open a terminal and enter the command: go mod init html-golang-scraper. This will create a new directory for your project. You can also create a new html-table-scraper.go file using the touch command, and install Colly with the command: go get -u github.com/gocolly/colly/….

Additional reading: Html Parser

Credit: youtube.com, The Ultimate Web Crawler Tutorial: Golang and Colly Made Easy

Here are the steps to initiate a new project:

  • Open a terminal and enter the command: go mod init html-golang-scraper.
  • Create a new html-table-scraper.go file using: touch html-table-scraper.go.
  • Install Colly with the command: go get -u github.com/gocolly/colly/…

If everything goes well, you should get the following output when running go run html-table-scraper.go: "/wp:paragraph wp:image {"id":3260",sizeSlug":"large"",linkDestination":"none"}".

Take a look at this: Go Html Parser

Install the Library

To install the Colly library, you can use the go get command. This command is mentioned in Example 1, where it's stated that you can install Colly using the go get command.

You'll need to have basic experience with Colly, a web scraping framework for Golang, and a clear understanding of Go basics. This is mentioned in Example 2, which also provides a step-by-step guide to initiating a new Go project.

The command to install Colly is: go get -u github.com/gocolly/colly/…

To verify that the installation was successful, you should get the following output when running go run html-table-scraper.go: /wp:paragraph wp:image {"id":3260",sizeSlug":"large"",linkDestination":"none"}.

Here is a summary of the installation process:

  • Open a terminal and enter the command: go get -u github.com/gocolly/colly/…
  • Create a new html-table-scraper.go file using: touch jack-scraper.go
  • Run the command: go run html-table-scraper.go to verify the installation

Retry

The Retry feature is a game-changer for when things don't go as planned.

In our system, Retry submits HTTP request again with the same parameters. This means that if a request fails, you can easily try again without having to re-enter all the details.

It's a simple yet effective way to handle errors and ensure that your requests are processed successfully.

Features and Functionality

Credit: youtube.com, How to build a web scraper using go and colly - golang + gocolly tutorial

Colly's features are impressive, and it's worth taking a closer look. It has a clean API, which makes it easy to work with and integrate into your projects.

One of the standout features is its speed, capable of handling over 1,000 requests per second on a single core. This makes it a great choice for large-scale web scraping tasks.

Colly also manages request delays and maximum concurrency per domain, which helps prevent overwhelming the server and getting blocked. Automatic cookie and session handling is also included, making it easier to maintain a consistent user experience.

Here are some of the key features summarized:

  • Clean API
  • Fast (>1k request/sec on a single core)
  • Manages request delays and maximum concurrency per domain
  • Automatic cookie and session handling
  • Sync/async/parallel scraping
  • Caching
  • Automatic encoding of non-unicode responses
  • Robots.txt support
  • Distributed scraping
  • Configuration via environment variables
  • Extensions

Features

Colly's features are impressive. It has a Clean API that makes it easy to use and integrate into your projects.

Colly is incredibly fast, capable of handling more than 1,000 requests per second on a single core. This makes it perfect for large-scale web scraping tasks.

Colly also manages request delays and maximum concurrency per domain, which helps prevent overwhelming websites with too many requests at once.

People Working in front of the Computer
Credit: pexels.com, People Working in front of the Computer

Automatic cookie and session handling is also a feature of Colly, making it easy to navigate websites that require users to log in or interact with them in some way.

Colly supports sync, async, and parallel scraping, giving you the flexibility to choose the approach that works best for your project.

Here are some of Colly's key features:

  • Clean API
  • Fast (>1k request/sec on a single core)
  • Manages request delays and maximum concurrency per domain
  • Automatic cookie and session handling
  • Sync/async/parallel scraping
  • Caching
  • Automatic encoding of non-unicode responses
  • Robots.txt support
  • Distributed scraping
  • Configuration via environment variables
  • Extensions

Colly also has a caching feature, which can help reduce the load on websites and improve the overall efficiency of your scraping process.

If you need help or want to report a bug, you can visit the issue tracker or join the #colly channel on freenode.

Constants

In our system, there are some constants that play a crucial role in how things work.

ProxyURLKey is the context key for the request proxy address, which is used to determine where to send requests.

Having a clear and consistent way to handle proxy addresses is essential for ensuring that requests are routed correctly.

This constant helps to streamline the process and prevent errors from occurring.

Consider reading: Proxy Api for Web Scraping

Functions

Credit: youtube.com, functions and features

Functions are a crucial part of the Colly library, allowing you to scrape data from websites in a variety of ways.

The "selector" function is required and uses a CSS (goquery) selector to identify the desired data. This selector is a powerful tool that can be used to target specific elements on a webpage.

The "attr" function is optional and allows you to select the matching element's attribute's value. If left blank or omitted, it will default to getting the text of the element.

Here are some key functions to know:

  • OnHTML registers a function that will be executed on every HTML element matched by the GoQuery Selector parameter.
  • OnResponse registers a function that will be executed on every response.
  • OnResponseHeaders registers a function that will be executed on every response when headers and status are already received, but body is not yet read.
  • OnScraped registers a function that will be executed as the final part of the scraping, after OnHTML and OnXML have finished.

These functions can be used to customize the scraping process and extract specific data from web pages. They can also be used to handle errors and exceptions that may occur during the scraping process.

Web Scraping

To get started with web scraping using Golang and Colly, you'll need to create a new Go file in your project directory and add the following code.

Credit: youtube.com, GO for Beginners - Web Scraping with Golang Tutorial

You'll create a new Colly collector named c with the line c := colly.NewCollector(). This collector is the core component of Colly, responsible for making HTTP requests and handling the scraping process.

The URL you want to scrape is defined with the line url := “http://quotes.toscrape.com/page/1/". You'll use this URL as the target for your scraper.

To extract text content from specific HTML elements, you'll use the e.ChildText() method within a callback function set up with c.OnHTML(.quote, func(e *colly.HTMLElement) {.

Inside the callback function, you can extract text content from HTML elements based on CSS selectors, such as quote := e.ChildText(“span.text”) or author := e.ChildText(“small.author”).

To clean up the extracted data, you can use strings.TrimSpace() to remove leading and trailing whitespace from each of the extracted strings.

To initiate the scraping process, you'll call c.Visit(url) to visit the specified URL.

Here are the basic steps to initiate your Go project:

  • Open a new directory in your IDE and create a new html-table-scraper.go file.
  • Run the command go mod init html-golang-scraper to initiate a new project.
  • Install Colly with the command go get -u github.com/gocolly/colly/….
  • Add the skeleton code to your html-table-scraper.go file to ensure the installation worked.

If everything goes well, you should get the following output when running go run html-table-scraper.go:

Credit: youtube.com, How to Build a Go Web Scraper with Colly

/wp:paragraph wp:image {"id":3260",sizeSlug":"large"",linkDestination":"none"}

To avoid issues with your IP address and web scraper being flagged and blocked, you can send your requests through ScraperAPI servers using their endpoint in the URL you're passing to the .Visit() method.

Here's how to do it:

1. Create a free ScraperAPI account to generate your API key and receive 5,000 API credits for free.

2. Add the following string to the beginning of the target URL: /wp:paragraph wp:syntaxhighlighter/code {"language":"xml"}

3. Set your timeout using the following code snippet: c.SetRequestTimeout(60 * time.Second)

4. Use the new Async Scraper service, which will submit a scraping job to ScraperAPI and return JSON data containing the raw HTML.

By following these steps, you can create a web scraper using Golang and Colly that extracts data from a website and avoids issues with your IP address and web scraper being flagged and blocked.

A fresh viewpoint: Golang Create

Configuration and Options

You can customize your Collector with a CollectorOption, which sets an option on the Collector.

There are several CollectorOptions available, but one key option is the AllowedDomains option, which sets the domain whitelist used by the Collector.

To further customize your Collector, you can use the SetClient method to override the previously set http.Client.

SetClient (*Client)

Credit: youtube.com, Using a client configuration

SetClient (*Client) is a method that allows you to override the previously set http.Client.

This method is used to change the http client used by the collector, and it will override any previously set client.

If you need to switch between different http clients, SetClient is the way to do it.

It's a simple yet effective way to customize the collector's behavior.

Option

Options are a crucial part of fine-tuning your Collector's behavior.

A CollectorOption is a specific type that sets an option on a Collector, giving you more control over its functionality.

You can use CollectorOptions to customize various aspects of your Collector, depending on its purpose and requirements.

The UserAgent function is a type of CollectorOption that sets the user agent used by the Collector, which can be useful for specific use cases or applications.

CollectorOptions can be a powerful tool in your toolkit, allowing you to tailor your Collector to meet your unique needs.

Allow Url Revisit

Three young people working on computers in a creative office with vibrant graffiti walls.
Credit: pexels.com, Three young people working on computers in a creative office with vibrant graffiti walls.

Allow Url Revisit is a crucial option in the Collector, and it allows multiple downloads of the same URL. This means you can revisit the same URL multiple times without any issues.

The Collector's behavior changes with this option, and it becomes more flexible in handling repeated downloads. This is especially useful when you need to download the same URL multiple times for different purposes.

By enabling Allow Url Revisit, you can avoid any potential issues that might arise from repeated downloads, such as server overload or data inconsistencies. This option ensures a smooth and efficient download process.

Allowed Domains

The Allowed Domains feature is a crucial part of the Collector's configuration.

It sets the domain whitelist used by the Collector.

You can think of it as a list of approved websites that the Collector can access.

This helps prevent the Collector from collecting data from unauthorized sources.

By whitelisting specific domains, you can ensure that the Collector only collects data from trusted websites.

This is especially important if you're working with sensitive information.

Detect Charset

Credit: youtube.com, Unicode, in friendly terms: ASCII, UTF-8, code points, character encodings, and more

Detect Charset is a feature that enables character encoding detection for non-utf8 response bodies without explicit charset declaration.

This feature uses the chardet library, which is a Python library that detects the character encoding of a given text.

DetectCharset is a powerful tool that can help you handle non-utf8 response bodies with ease, and it's especially useful when you're working with legacy systems or third-party APIs that don't specify the charset.

The chardet library is a well-maintained and widely-used library that can detect over 40 different character encodings, making it a reliable choice for this feature.

By using DetectCharset, you can ensure that your application can handle a wide range of character encodings, and provide a better experience for your users.

Discover more: C# Web Scraper Library

Cookies and Authentication

Cookies are an essential part of web scraping with GoLang Colly, as they allow you to persist cookies between requests.

The Cookies function returns the cookies to send in a request for a given URL.

To manage cookies, you can use the SetCookieJar function, which overrides the previously set cookie jar.

SetCookieJar is a powerful tool for maintaining cookies and ensuring that your scraper can handle complex websites that require cookies to function properly.

Testing and Debugging

Credit: youtube.com, Testing and Debugging in GoLang

Testing and debugging are crucial steps in the web scraping process, and Colly makes it easy to handle these tasks.

You can add callbacks to the collector object to print the URL Colly is visiting, giving you visual feedback on what's happening.

To keep these callbacks organized, save them on a file so you can copy them to your projects as boilerplate code.

The collector object will call upon each of these functions at the right stage of the web scraping job, allowing you to focus on what matters: the data.

A successful request is indicated by a status code, which you can print using a callback.

Related reading: No Code Web Scraper

Callbacks for Testing

Callbacks are a great way to add functionality to your web scraper without cluttering your code.

You can save your callbacks in a file so you can easily copy them into your projects as boilerplate code.

The collector object in Colly makes it easy to handle requests and callbacks.

Colorful lines of code on a computer screen showcasing programming and technology focus.
Credit: pexels.com, Colorful lines of code on a computer screen showcasing programming and technology focus.

You can start by adding a few callbacks on top of the c object.

The first callback will print the URL Colly is visiting, giving you visual feedback on what's happening when scraping multiple pages.

You can also add a callback to print the status code, letting you know whether or not the request was successful.

An error handler is also a good idea to catch any issues that might arise.

The collector object will call upon each of these functions at the right stage of the web scraping job.

Test Run: Go Web Scraper Code

To run a test with your Go web scraper, you'll need to use a custom timeout or ignore it altogether. If you're using ScraperAPI, you'll also need to add your API key to the .Visit() method.

You can run your Go web scraper by visiting the target website, extracting tabular data from the main table, and writing it to an employees.json file. This process involves making HTTP requests and handling the scraping process using a collector.

Credit: youtube.com, Web Scraping with GO... Easy AND Fast?!

To initiate the scraping process, you'll need to call c.Visit(url) with the specified URL. If there's an error during the scraping process, you can use log.Fatal(err) to log the error and exit the program.

Here's a step-by-step guide to setting up your Go project:

  • Open a new directory in Visual Studio or your preferred IDE.
  • Initiate a new project by running the command `go mod init html-golang-scraper` in the terminal.
  • Create a new html-table-scraper.go file using the command `touch jack-scraper.go`.
  • Install Colly using the command `go get -u github.com/gocolly/colly/…`.

If everything works correctly, you should see the following output when running `go run html-table-scraper.go`:

`/wp:paragraph /wp:image {"id":3260",sizeSlug":"large"",linkDestination":"none"}`

Advanced Topics

In Go, you can use the `colly` package to handle cookies in a more advanced way. The `colly` package provides a built-in mechanism to handle cookies, which is essential for scraping websites that require authentication.

You can use the `Request` method to specify the URL of the website, and the `Cookies` method to add cookies to the request. For example, you can use the `colly.Collect` function to add a cookie to the request.

By using the `colly` package, you can also set a custom cookie jar to store and manage cookies for you. This can be particularly useful when scraping websites that use complex cookie handling.

OnXMLDetach

Close Up Shot of  Robot Toys
Credit: pexels.com, Close Up Shot of Robot Toys

OnXMLDetach is a powerful tool that allows you to deregister a function.

By using OnXMLDetach, you can ensure that a function will not be executed after it's been detached.

This is useful when you need to temporarily disable a function without deleting it entirely.

Deregistering a function with OnXMLDetach prevents it from being executed, giving you more control over your code's behavior.

This can be especially helpful in situations where you're working with complex systems and need to isolate specific functions for testing or debugging purposes.

OnXMLDetach is a straightforward way to achieve this level of control and flexibility in your code.

Wait

The Wait function is a crucial part of managing collector jobs. It returns when the collector jobs are finished.

If you're working with a Collector, you'll want to use the Wait function to ensure your jobs are completed before moving on. This is especially important if you're relying on the output of these jobs to inform your next steps.

The Wait function is designed to be efficient, allowing you to move on with your workflow once the collector jobs are done.

Error Handling and Limits

Credit: youtube.com, Golang Error Handling Isn't ACTUALLY Bad

You can register a function to be executed if an error occurs during the HTTP request using the OnError method.

This allows you to catch and handle errors in a custom way, making your code more robust and reliable.

The OnError method is a powerful tool for error handling, and it's easy to use - just pass in a function that will be executed when an error happens.

Error for Already Visited added in v2.2.0

In v2.2.0, a new error type was added to handle already visited URLs. This error type is called AlreadyVisitedError.

AlreadyVisitedError is returned synchronously by Visit when the URL passed to Visit is already visited. This error appears in the OnError callback when an already visited URL is encountered after following redirects.

The AlreadyVisitedError can also be returned by Visit if Async mode is not enabled. This is a change that developers should be aware of when working with Visit in non-async mode.

To handle this error, you can register a function using the OnError method of the Collector. This function will be executed if an error occurs during the HTTP request, including the AlreadyVisitedError.

A unique perspective: Golang Asynchronous

OnError

A Person Holding a Scraper
Credit: pexels.com, A Person Holding a Scraper

OnError is a crucial part of error handling in this system. It registers a function that will be executed if an error occurs during the HTTP request.

This function can be used to handle unexpected errors and prevent them from crashing the entire system. The function will be called when an error happens, allowing you to take corrective action.

OnError is registered on the Collector object, making it a part of the Collector's collecting job. This means that any errors that occur during the collecting job will trigger the registered function.

Additional reading: Golang Errors

Limits

The Limits feature allows you to add new rules to a collector. This can be done through the Collector's Limits method.

There are two ways to add new LimitRules to a collector, either by using the Limit method or the Limits method. The Limit method adds a single new LimitRule, while the Limits method adds multiple new LimitRules.

You can use the Limits method to add multiple new LimitRules to a collector at once. This can be more efficient than using the Limit method multiple times.

By adding new LimitRules to a collector, you can control what data is collected and when. This is an important part of error handling and ensuring that your data collection process is robust and reliable.

Emanuel Anderson

Senior Copy Editor

Emanuel Anderson is a meticulous and detail-oriented Copy Editor with a passion for refining the written word. With a keen eye for grammar, syntax, and style, Emanuel ensures that every article that passes through their hands meets the highest standards of quality and clarity. As a seasoned editor, Emanuel has had the privilege of working on a diverse range of topics, including the latest developments in Space Exploration News.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.