To install Colly in your Go project, you need to have Go installed on your system. Once you have Go set up, you can easily install the Colly library using Go's package management tool called go get
.
Here are the steps to install Colly in your Go project:
Open Your Terminal: Open a terminal window on your system.
Navigate to Your Go Project: Use the
cd
command to navigate to your Go project's directory. If you haven't created a project yet, you can create one using thego mod init <project-name>
command.
cd path/to/your/project
If you need to initialize a new module because you haven't done so already, run:
go mod init your_project_name
- Install Colly: Run the following command to install the Colly package:
go get github.com/gocolly/colly/v2
This command will download the Colly package and its dependencies and add them to your project's go.mod
file.
- Check Installation: To verify that Colly has been successfully installed, you can check your
go.mod
file to see if it includes a reference togithub.com/gocolly/colly
.
Here is an example of how your go.mod
file might look after installing Colly:
module your_project_name
go 1.17
require (
github.com/gocolly/colly/v2 v2.1.0 // indirect
)
- Use Colly in Your Code: Now you can start using Colly in your Go project. Here's a simple example of how to use Colly to scrape a website:
package main
import (
"fmt"
"github.com/gocolly/colly/v2"
)
func main() {
c := colly.NewCollector()
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})
c.Visit("http://httpbin.org/")
}
This code sets up a new Colly collector, defines a callback for HTML elements that match the CSS selector a[href]
(which targets anchor tags with an href
attribute), and starts scraping the website at http://httpbin.org/
. When anchor tags are found, it prints out the text and link to the console.
Remember to handle errors appropriately in your real-world applications, although it is omitted here for brevity.
That's it! You have successfully installed Colly and used it in a simple Go project.