sw-jobs-scraper/main.go

315 lines
8.1 KiB
Go
Raw Permalink Normal View History

2024-06-08 14:11:40 +00:00
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
2024-06-11 06:49:46 +00:00
"time"
2024-06-08 14:11:40 +00:00
"github.com/gocolly/colly"
)
type job struct {
2024-06-11 10:08:58 +00:00
Title string `json:"title"`
Logo string `json:"logo"`
Company string `json:"company"`
Location string `json:"location"`
Type string `json:"type"`
Description string `json:"description"`
Link string `json:"link"`
Skills skills `json:"skills"`
Scraped string `json:"scraped"`
Source string `json:"source"`
2024-06-08 14:11:40 +00:00
}
type skills struct {
React bool `json:"react"`
Python bool `json:"python"`
Golang bool `json:"golang"`
Svelte bool `json:"svelte"`
Nextjs bool `json:"nextjs"`
Typescript bool `json:"typescript"`
2024-06-08 19:12:04 +00:00
Tailwind bool `json:"tailwind"`
2024-06-08 14:11:40 +00:00
}
2024-06-11 09:38:05 +00:00
// Utility functions
// Checks if a string contains any of the given keywords
2024-06-08 14:11:40 +00:00
func skillChecker(description string) skills {
return skills{
React: strings.Contains(description, "React"),
Python: strings.Contains(description, "Python"),
2024-06-08 19:12:04 +00:00
Golang: strings.Contains(description, "Golang"),
2024-06-08 14:11:40 +00:00
Svelte: strings.Contains(description, "Svelte"),
Nextjs: strings.Contains(description, "Next.js"),
Typescript: strings.Contains(description, "TypeScript"),
2024-06-08 19:12:04 +00:00
Tailwind: strings.Contains(description, "Tailwind"),
2024-06-08 14:11:40 +00:00
}
}
2024-06-11 09:38:05 +00:00
// Converts job struct to json
func jobsToJson(file *os.File, jobs []job, fName string) {
// Encode jobs slice to JSON
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ") // Pretty-print with indentation
if err := encoder.Encode(jobs); err != nil {
log.Fatalf("Cannot write to file %q: %s", fName, err)
}
fmt.Println("Job details successfully written to", fName)
}
2024-06-08 14:11:40 +00:00
2024-06-11 09:38:05 +00:00
func checkIfPaid(description string) {
for _, keyword := range unpaidKeywords {
if strings.Contains(strings.ToLower(description), keyword) {
return
}
}
}
func checkIfStudent(description string) string {
for _, keyword := range studentKeywords {
if strings.Contains(strings.ToLower(description), keyword) {
return "student"
}
}
return "full time"
}
2024-06-08 14:11:40 +00:00
// Slice to store job details
var (
2024-12-09 08:56:26 +00:00
excluded = []string{"senior", "lead", "founder", "cto", "vp of", "erfaren", "arkitekt", "architect", "manager", "ulønnet", "unpaid", "praktik", "cyber", "leder", "sikkerhed", "supporter", "sr."}
2024-06-11 09:38:05 +00:00
unpaidKeywords = []string{"unpaid", "praktik", "ulønnet"}
studentKeywords = []string{"studerende", "studenter", "student", "medhjælper"}
)
2024-06-08 14:11:40 +00:00
2024-06-11 06:49:46 +00:00
func scrapeHub() {
2024-06-11 09:38:05 +00:00
var (
jobs []job
jobCount int
2024-12-06 22:28:03 +00:00
fName = "/app/data/thehub.json"
2024-06-19 18:26:36 +00:00
maxJobs = 20
2024-06-11 09:38:05 +00:00
baseUrl = "https://thehub.io"
searchString = "https://thehub.io/jobs?roles=frontenddeveloper&roles=fullstackdeveloper&roles=backenddeveloper&roles=devops&paid=true&countryCode=DK&sorting=newJobs"
)
2024-06-08 14:11:40 +00:00
2024-12-13 14:01:00 +00:00
// Create file after scraping is complete
2024-06-08 14:11:40 +00:00
c := colly.NewCollector(
colly.AllowedDomains("www.thehub.io", "thehub.io"),
)
2024-06-19 18:26:36 +00:00
detailsCollector := colly.NewCollector(
colly.AllowedDomains("www.thehub.io", "thehub.io"),
2024-12-06 21:13:38 +00:00
colly.CacheDir("/app/data/thehub_cache"),
2024-06-19 18:26:36 +00:00
)
2024-12-13 14:01:00 +00:00
2024-06-08 14:11:40 +00:00
c.OnHTML("div[class=card__content]", func(e *colly.HTMLElement) {
2024-06-11 09:38:05 +00:00
if jobCount >= maxJobs {
return
}
2024-06-08 14:11:40 +00:00
title := e.ChildText("span.card-job-find-list__position")
for _, excludedWord := range excluded {
if strings.Contains(strings.ToLower(title), excludedWord) {
return
}
}
link := e.ChildAttr("a", "href")
fullLink := baseUrl + link
detailsCollector.Visit(fullLink)
})
detailsCollector.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})
detailsCollector.OnHTML("div[class='view-job-details']", func(e *colly.HTMLElement) {
2024-12-13 14:01:00 +00:00
if jobCount >= maxJobs {
return
}
2024-06-08 14:11:40 +00:00
logo := e.ChildAttr("div.media-item__image", "style")
cutLeft := "background-image:url("
cutRight := ");"
trimmedLogo := strings.Trim(logo, cutLeft+cutRight)
2024-06-09 10:40:42 +00:00
descriptionHTML, err := e.DOM.Find("content.text-block__content > span").Html()
if err != nil {
log.Printf("Error getting HTML of description: %s", err)
return
}
2024-06-11 09:38:05 +00:00
2024-06-08 14:11:40 +00:00
jobDetails := job{
Title: e.ChildText("h2[class=view-job-details__title]"),
Logo: trimmedLogo,
Company: e.ChildText(".bullet-inline-list > a:first-child"),
Location: e.ChildText(".bullet-inline-list > a:nth-child(2)"),
Type: e.ChildText(".bullet-inline-list > a:nth-child(3)"),
2024-06-09 10:40:42 +00:00
Description: descriptionHTML,
2024-06-08 14:11:40 +00:00
Link: e.Request.URL.String(),
Skills: skillChecker(e.ChildText("content.text-block__content > span")),
2024-06-11 10:08:58 +00:00
Scraped: time.Now().String(),
2024-06-11 09:38:05 +00:00
Source: baseUrl,
2024-06-08 14:11:40 +00:00
}
jobs = append(jobs, jobDetails)
jobCount++
2024-12-13 14:01:00 +00:00
fmt.Printf("Scraped job %d from TheHub\n", jobCount)
})
c.OnHTML("a.page-link", func(e *colly.HTMLElement) {
2024-06-11 09:38:05 +00:00
if jobCount >= maxJobs {
return
}
2024-06-08 14:11:40 +00:00
nextPage := e.Attr("href")
if nextPage != "" {
fullNextPage := baseUrl + nextPage
e.Request.Visit(fullNextPage)
}
})
2024-12-13 14:01:00 +00:00
// Add error handling for the initial visit
err := c.Visit(searchString)
if err != nil {
log.Printf("Error visiting TheHub: %s", err)
return
}
// Wait for all collectors to finish
c.Wait()
detailsCollector.Wait()
2024-06-08 14:11:40 +00:00
2024-12-13 14:01:00 +00:00
// Write jobs to file after scraping is complete
if len(jobs) > 0 {
file, err := os.Create(fName)
if err != nil {
log.Printf("Cannot create file %q: %s", fName, err)
return
}
defer file.Close()
jobsToJson(file, jobs, fName)
fmt.Printf("Successfully scraped %d jobs from TheHub\n", len(jobs))
} else {
log.Println("No jobs were scraped from TheHub")
}
2024-06-08 14:11:40 +00:00
}
2024-06-08 20:12:22 +00:00
2024-06-11 09:38:05 +00:00
func scrapeItJobBank() {
var (
jobs []job
jobCount int
2024-12-06 22:28:03 +00:00
fName = "/app/data/it-jobbank.json"
2024-06-19 18:26:36 +00:00
maxJobs = 20
2024-06-11 09:38:05 +00:00
baseUrl = "https://www.it-jobbank.dk"
2024-06-11 10:08:58 +00:00
searchString = "https://www.it-jobbank.dk/jobsoegning/udvikling"
2024-06-11 09:38:05 +00:00
)
c := colly.NewCollector(
colly.AllowedDomains("www.it-jobbank.dk", "it-jobbank.dk"),
)
2024-06-11 06:49:46 +00:00
2024-06-19 18:26:36 +00:00
detailsCollector := colly.NewCollector(
colly.AllowedDomains("www.it-jobbank.dk", "it-jobbank.dk"),
2024-12-13 14:01:00 +00:00
colly.CacheDir("/app/data/itjobbank_cache"),
)
2024-06-19 18:26:36 +00:00
2024-06-11 09:38:05 +00:00
c.OnHTML("div[class=result]", func(e *colly.HTMLElement) {
if jobCount >= maxJobs {
return
}
2024-12-13 14:01:00 +00:00
2024-06-11 09:38:05 +00:00
title := e.ChildText("h3.job-title > a")
for _, excludedWord := range excluded {
if strings.Contains(strings.ToLower(title), excludedWord) {
return
}
}
fullLink := e.ChildAttr("h3.job-title > a", "href")
detailsCollector.Visit(fullLink)
})
detailsCollector.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})
detailsCollector.OnHTML("section > div", func(e *colly.HTMLElement) {
2024-12-13 14:01:00 +00:00
if jobCount >= maxJobs {
return
}
2024-06-11 09:38:05 +00:00
descriptionHTML, err := e.DOM.Find("div[id=job_ad]").Html()
if err != nil {
log.Printf("Error getting HTML of description: %s", err)
return
}
2024-12-13 14:01:00 +00:00
2024-06-11 09:38:05 +00:00
checkIfPaid(descriptionHTML)
2024-12-13 14:01:00 +00:00
2024-06-11 09:38:05 +00:00
title := e.ChildText("h1.title")
if title == "" {
title = e.ChildText("h1[id=jobtitle]")
}
2024-12-13 14:01:00 +00:00
2024-06-11 09:38:05 +00:00
jobDetails := job{
Title: title,
2024-06-11 10:44:02 +00:00
Logo: baseUrl + e.ChildAttr("div.company-logo > img", "src"),
2024-06-11 09:38:05 +00:00
Company: e.ChildText("p.published"),
Location: e.ChildText("div.job-location > p.caption"),
Type: checkIfStudent(descriptionHTML),
Description: descriptionHTML,
Link: e.Request.URL.String(),
Skills: skillChecker(descriptionHTML),
2024-06-11 10:08:58 +00:00
Scraped: time.Now().String(),
2024-06-11 09:38:05 +00:00
Source: baseUrl,
}
jobs = append(jobs, jobDetails)
jobCount++
2024-12-13 14:01:00 +00:00
fmt.Printf("Scraped job %d from IT JobBank\n", jobCount)
2024-06-11 09:38:05 +00:00
})
2024-12-13 14:01:00 +00:00
2024-06-11 09:38:05 +00:00
c.OnHTML("a.page-link", func(e *colly.HTMLElement) {
if jobCount >= maxJobs {
return
}
nextPage := e.Attr("href")
if nextPage != "" {
e.Request.Visit(nextPage)
}
})
2024-12-13 14:01:00 +00:00
// Add error handling for the initial visit
err := c.Visit(searchString)
if err != nil {
log.Printf("Error visiting IT JobBank: %s", err)
return
}
// Wait for all collectors to finish
c.Wait()
detailsCollector.Wait()
// Write jobs to file after scraping is complete
if len(jobs) > 0 {
file, err := os.Create(fName)
if err != nil {
log.Printf("Cannot create file %q: %s", fName, err)
return
}
defer file.Close()
jobsToJson(file, jobs, fName)
fmt.Printf("Successfully scraped %d jobs from IT JobBank\n", len(jobs))
} else {
log.Println("No jobs were scraped from IT JobBank")
}
2024-06-11 09:38:05 +00:00
}
2024-06-11 06:49:46 +00:00
2024-06-08 20:12:22 +00:00
func main() {
2024-06-11 06:49:46 +00:00
scrapeHub()
2024-06-11 09:38:05 +00:00
scrapeItJobBank()
2024-06-08 20:12:22 +00:00
}