Created
April 24, 2018 08:03
-
-
Save tranphuoctien/7f716ffafabd64d0e34463d62a36b8c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Scraping implementation using channels | |
| // go run scrap.go | |
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "net/http" | |
| "os" | |
| "sync" | |
| ) | |
| type Result struct { | |
| site string | |
| page []byte | |
| } | |
| func getPage(link string, c chan Result, wg *sync.WaitGroup) { | |
| defer wg.Done() | |
| resp, err := http.Get(link) | |
| if err != nil { | |
| fmt.Println("error") | |
| os.Exit(1) | |
| } | |
| defer resp.Body.Close() | |
| data, _ := ioutil.ReadAll(resp.Body) | |
| res := new(Result) | |
| res.site = link | |
| res.page = data | |
| c <- *res | |
| } | |
| func main() { | |
| links := []string{ | |
| "http://www.detik.com", | |
| "http://www.kompas.com", | |
| "http://news.metrotvnews.com", | |
| "http://okezone.com", | |
| "http://www.liputan6.com", | |
| "http://www.jawapos.com", | |
| "http://www.viva.co.id", | |
| "http://www.republika.co.id/", | |
| "http://www.tribunnews.com/", | |
| "http://www.antaranews.com/", | |
| } | |
| c := make(chan Result) | |
| wg := &sync.WaitGroup{} | |
| for index := range links { | |
| wg.Add(1) | |
| go getPage(links[index], c, wg) | |
| } | |
| go func() { | |
| wg.Wait() | |
| close(c) | |
| }() | |
| for res := range c { | |
| fmt.Println(res.site, ":", string(res.page)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment