1 Star 0 Fork 1

snow2zhou / colly

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
local_files.go 715 Bytes
一键复制 编辑 原始数据 按行查看 历史
Vincent Smith 提交于 2018-06-22 13:59 . Added local files example
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/gocolly/colly"
)
func main() {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
t := &http.Transport{}
t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
c := colly.NewCollector()
c.WithTransport(t)
pages := []string{}
c.OnHTML("h1", func(e *colly.HTMLElement) {
pages = append(pages, e.Text)
})
c.OnHTML("a", func(e *colly.HTMLElement) {
c.Visit("file://" + dir + "/html" + e.Attr("href"))
})
fmt.Println("file://" + dir + "/html/index.html")
c.Visit("file://" + dir + "/html/index.html")
c.Wait()
for i, p := range pages {
fmt.Printf("%d : %s\n", i, p)
}
}
1
https://gitee.com/snow2zhou/colly.git
git@gitee.com:snow2zhou/colly.git
snow2zhou
colly
colly
v1.2.0

搜索帮助