Web 爬蟲:scrape

jopen 9年前發布 | 15K 次閱讀 scrape 網絡爬蟲

scrape 是一個使用 Go 語言開發的簡單高級Web 爬蟲。

示例代碼:

package main

import (
    "fmt"
    "net/http"

    "github.com/yhat/scrape"
    "golang.org/x/net/html"
    "golang.org/x/net/html/atom"
)

func main() {
    // request and parse the front page
    resp, err := http.Get("https://news.ycombinator.com/")
    if err != nil {
        panic(err)
    }
    root, err := html.Parse(resp.Body)
    if err != nil {
        panic(err)
    }

    // define a matcher
    matcher := func(n *html.Node) bool {
        // must check for nil values
        if n.DataAtom == atom.A && n.Parent != nil && n.Parent.Parent != nil {
            return scrape.Attr(n.Parent.Parent, "class") == "athing"
        }
        return false
    }
    // grab all articles and print them
    articles := scrape.FindAll(root, matcher)
    for i, article := range articles {
        fmt.Printf("%2d %s (%s)\n", i, scrape.Text(article), scrape.Attr(article, "href"))
    }
}

項目主頁:http://www.baiduhome.net/lib/view/home/1432522312785

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!