package main
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
)
func Get(url string) (result string, err error) {
resp, err1 := http.Get(url)
if err != nil {
err = err1
return
}
defer resp.Body.Close()
buf := make([]byte, 4*1024)
for true {
n, err := resp.Body.Read(buf)
if err != nil {
if err == io.EOF {
fmt.Println("完成")
break
} else {
fmt.Println("resp.Body.Read err = ", err)
break
}
}
result += string(buf[:n])
}
return
}
func SpiderPage(i int, page chan<- int) {
url := "https://github.com/search?q=go&type=Repositories&p=1" + strconv.Itoa((i-1)*50)
fmt.Printf("正在爬取第%d個網頁\n", i)
result, err := Get(url)
if err != nil {
fmt.Println("http.Get err = ", err)
return
}
filename := "page"+strconv.Itoa(i) + ".html"
f, err1 := os.Create(filename)
if err1 != nil {
fmt.Println("os.Create err = ", err1)
return
}
f.WriteString(result)
f.Close()
page <- i
}
func Run(start, end int) {
fmt.Printf("正在爬取第%d頁到%d頁\n", start, end)
page := make(chan int)
for i := start; i <= end; i++ {
go SpiderPage(i, page)
}
for i := start; i <= end; i++ {
fmt.Printf("第%d個頁面爬取完成\n", <-page)
}
}
func main() {
var start, end int
fmt.Printf("請輸入起始頁數字>=1:>")
fmt.Scan(&start)
fmt.Printf("請輸入結束頁數字:>")
fmt.Scan(&end)
Run(start, end)
}