欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

go-爬虫-百度贴吧(并发版)

程序员文章站 2024-01-29 13:59:46
爬取百度贴吧的网页 非并发版 并发版 这个只是在上面的基础上加了管道和开了协程 ......

爬取百度贴吧的网页

非并发版

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
    "strconv"
)

func httpget(url string) (result string, err error) {
    //  var result string
    resp, err1 := http.get(url)
    if err1 != nil {
        err = err1
        return
    }
    defer resp.body.close()

    buf := make([]byte, 4096)
    for {
        n, err2 := resp.body.read(buf)
        if n == 0 {
            fmt.println("读取网页完成")
            break
        }
        if err2 != nil && err2 != io.eof {
            err = err2
            return
        }
        result += string(buf[:n])
    }
    return
}

func working(start, end int) {
    fmt.printf("正在爬取第%d到第%d页", start, end)
    //爬取每一个网页
    for i := start; i <= end; i++ {
        url := "https://tieba.baidu.com/f?kw=vue&ie=utf-8&pn=" + strconv.itoa((i-1)*50)
        result, err := httpget(url)
        if err != nil {
            fmt.println("httpget err", err)
            continue
        }
        //      fmt.println("result", result)
        f, err := os.create("第" + strconv.itoa(i) + "页" + ".html")
        if err != nil {
            fmt.println("httpget err", err)
            continue
        }
        f.writestring(result)
        f.close()
    }
}

func main() {
    var start, end int
    fmt.print("请输入起始页。。。")
    fmt.scan(&start)
    fmt.print("请输入终止页。。。")
    fmt.scan(&end)

    working(start, end)
}

并发版

这个只是在上面的基础上加了管道和开了协程

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
    "strconv"
)

func httpget(url string) (result string, err error) {
    //  var result string
    resp, err1 := http.get(url)
    if err1 != nil {
        err = err1
        return
    }
    defer resp.body.close()

    buf := make([]byte, 4096)
    for {
        n, err2 := resp.body.read(buf)
        if n == 0 {
            fmt.println("读取网页完成\n")
            break
        }
        if err2 != nil && err2 != io.eof {
            err = err2
            return
        }
        result += string(buf[:n])
    }
    return
}

func spiderpage(index int, page chan int) {
    fmt.printf("正在爬取第%d到页\n", index)
    //爬取每一个网页
    //  for i := start; i <= end; i++ {
    url := "https://tieba.baidu.com/f?kw=vue&ie=utf-8&pn=" + strconv.itoa((index-1)*50)
    result, err := httpget(url)
    if err != nil {
        fmt.println("httpget err", err)
        return
    }
    //      fmt.println("result", result)
    f, err := os.create("第" + strconv.itoa(index) + "页" + ".html")
    if err != nil {
        fmt.println("httpget err", err)
        return
    }
    f.writestring(result)
    f.close()
    //  }

    page <- index
}

func working2(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.print("请输入起始页。。。")
    fmt.scan(&start)
    fmt.print("请输入终止页。。。")
    fmt.scan(&end)

    working2(start, end)
}