# Web Scrapper (goquery)

Bu yazıda Go dilinde nasıl basitçe web scrapper yapacağımıza bakacağız.

### Web Scrapper Nedir?

Web Scrapper bir web sayfasındaki elementleri işleyen araçtır.

**Örnek uygulama:**

**blog.golang.org** sitesindeki blog başlıklarını listeleyen Go programının yazılması.

```go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/PuerkitoBio/goquery"
)

func main() {
	blogBasliklari, err := baslikCek("https://blog.golang.org")
	if err != nil {
		log.Println(err)
	}
	fmt.Println("Başlıklar:")
	fmt.Printf(blogBasliklari)
}

// URL adresinden blog başlıklarını çekecek fonksiyon
func baslikCek(url string) (string, error) {

	// HTML'i çek
	resp, err := http.Get(url)
	if err != nil {
		return "", err
	}

	// goquery dökümanına çevir
	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		return "", err
	}

	// liste oluştur
	basliklar := ""
	doc.Find(".title a").Each(func(i int, s *goquery.Selection) {
		basliklar += "- " + s.Text() + "\n"
	})
	return basliklar, nil
}

```

Açıklaması:\
goquery kütüphanesini bilgisayarımıza indiriyoruz.

> go get github.com/PuerkitoBio/goquery

`baslikCek` fonksiyonuna URL adresini girdik. Zaten bu fonksiyonu da bi oluşturduk. Hata kontrolü yaptıktan sonra başlıkları yazdırdık.

`baslikCek` fonksiyonuna baktığımızda;\
İlk önce url adresini, yani içindeki elementleri, çektik. goquery dökümanına çevirdik. Burada dikkat edilmesi gereken nokta, `resp` değişkeni bizim çektiğimiz url adresidir. Daha sonra liste olarak oluşturduk. Liste oluşturma işleminde `.title` sınıfına ait ve `a` etiketinde olan elementleri sıralamasını istedik. Element seçim işlemi jQuery selector mantığında çalışır.

Çıktımız:

{% hint style="info" %}
Başlıklar:

* Announcing the 2019 Go Developer Survey
* Go.dev: a new hub for Go developers
* Go Turns 10
* Go Modules: v2 and Beyond
* Working with Errors in Go 1.13
  {% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://go.kaanksc.com/bolum-7-dosya-islemleri/web-scrapper.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
