Avançar para o conteúdo principal

How to Use the SerpApi API with Go: Complete Tutorial for Beginners# How to Use the SerpApi API with

How to Search Google Jobs in Real-Time with Go and SerpApi

  •Suggested slug: `go-serpapi-google-jobs-tutorial` •Full Post:* > Searching for jobs manually on Google Jobs is slow. What if you could monitor "Golang Developer" jobs in Luanda, Lisbon, and remote with just a few lines of Go? > > I'm °Pires  Aurélio , a Go Developer Advocate focused on the Lusophone community. I work 100% with written, async communication, which is why my focus is on crystal-clear documentation. > > •LinkedIn: https://www.linkedin.com/in/pires-aurélio-511330385 > •Full code: https://github.com/Aureliopires186/go-serpapi-examples > > •Why SerpApi instead of direct scraping? > > •If you try to do `http.Get` on Google, you will get a CAPTCHA in 2 minutes. SerpApi solves that and returns clean JSON, with official Go support. > > • Practical Tutorial > > In my repository, I created the `01-google-jobs` example that is already production-ready. > > •1. Set your API Key: > ```bash > export SERPAPI_KEY=your...

Como usar a API SerpApi com Go: Tutorial Completo para Iniciantes [PT-BR]


CONTEÚDO PRA COPIAR E COLAR:



# Como usar a API SerpApi com Go: Tutorial Completo para Iniciantes


Olá devs! Hoje vou mostrar como integrar a SerpApi no Go em menos de 10 minutos. 

Se você é de língua portuguesa e quer extrair dados do Google sem dor de cabeça, esse tutorial é pra você.


## O que é SerpApi?


SerpApi é uma API que extrai dados do Google, Bing, Amazon e outros mecanismos de busca. 

Ela resolve CAPTCHAs e acompanha mudanças de layout pra você. Clientes incluem Nvidia, Shopify e Adobe.


## Passo 1: Pegar a API Key


1. Acesse: serpapi.com 

2. Crie uma conta gratuita. Você ganha 100 buscas/mês.


## Passo 2: Criar o Projeto em Go


Crie 2 arquivos:


### 1. `go.mod`

go

module github.com/seuuser/serpapi-go-search


go 1.21



### 2. `main.go`

go

package main


import (

 "encoding/json"

 "fmt"

 "io"

 "log"

 "net/http"

 "net/url"

 "os"

)


type SearchResult struct {

 OrganicResults []OrganicResult json:"organic_results"

}


type OrganicResult struct {

 Title string json:"title"

 Link string json:"link"

 Snippet string json:"snippet"

}


func main() {

 apiKey := os.Getenv("SERPAPI_KEY")

 if apiKey == "" {

  log.Fatal("Erro: Defina SERPAPI_KEY. Ex: export SERPAPI_KEY=sua_key")

 }

 query := "vagas de desenvolvedor Go Brasil"

 fmt.Printf("Buscando por: %s\n", query)

 results, err := searchGoogle(query, apiKey)

 if err!= nil {

  log.Fatal(err)

 }


 for i, result := range results.OrganicResults {

  if i >= 3 { break }

  fmt.Printf("%d. %s\n", i+1, result.Title)

  fmt.Printf(" %s\n", result.Link)

  fmt.Printf(" %s\n", result.Snippet)

 }

}


func searchGoogle(query string, apiKey string) (*SearchResult, error) {

 params := url.Values{}

 params.Add("engine", "google")

 params.Add("q", query)

 params.Add("api_key", apiKey)

 params.Add("hl", "pt") // Resultados em português

 resp, err := http.Get("https://serpapi.com/search?" + params.Encode())

 if err!= nil {

  return nil, err

 }

 defer resp.Body.Close()


 body, _ := io.ReadAll(resp.Body)

 var result SearchResult

 json.Unmarshal(body, &result)

 return &result, nil

}



## Passo 3: Rodar o Projeto


No terminal:

bash

export SERPAPI_KEY=sua_api_key_aqui

go run main.go



## Código Completo no GitHub


Deixei o projeto completo aqui: [github.com/Aureliopires186/SerpApi-Go-Search](https://github.com/Aureliopires186/SerpApi-Go-Search)


## Conclusão


Com 30 linhas de Go você já consegue extrair dados do Google de forma profissional. 

A SerpApi é perfeita pra devs que querem focar no produto e não em resolver CAPTCHA.


Se você é dev de língua portuguesa e tem dúvidas, me chama no Telegram: [@PiresAurelio](https://t.me/PiresAurelio)


Qual próximo tutorial vocês querem? Comenta aí 👇


#go #golang #serpapi #api #programacao #portugues


Como usar a API SerpApi com Go: Tutorial Completo para Iniciantes [PT-BR]

Comentários

Pires Aurélio