zoukankan      html  css  js  c++  java
  • [日常] Go语言*-匿名函数习题2

    练习5.13: 修改crawl,使其能保存发现的页面,必要时,可以创建目录来保存这些页面。只保存来自原始域名下的页面。假设初始页面在golang.org下,就不
    要保存vimeo.com下的页面。

    package main
    
    import (
            "fmt"
            "io"
            "io/ioutil"
            "links"
            "log"
            "net/http"
            "net/url"
            "os"
    )
    /*
    练习5.13: 修改crawl,使其能保存发现的页面,必要时,可以创建目录来保存这些页面。只保存来自原始域名下的页面。假设初始页面在golang.org下,就不要保存vimeo.com下的页面。
    */
    var sum int
    func main() {
            breadthFirst(crawl, os.Args[1:])
    }
    
    /*
    抓取页面的所有连接
    */
    func crawl(url string) []string {
            sum++
    
            go save(url)
            fmt.Printf("%d|%s
    ", sum, url)
            list, err := links.Extract(url)
            if err != nil {
                    log.Print(err)
            }
            return list
    }
    
    /*
    保存页面到文件
    */
    func save(u string) bool {
    
            urlObj, _ := url.Parse(u)
            path := "/tmp/crawl/" + urlObj.Host
            if urlObj.Path == "" || urlObj.Path == "/" {
                    urlObj.Path = "/index.html"
            }
            filename := path + urlObj.Path //重点注意文件名
            fmt.Println(filename)
            //打开文件
            f, _ := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0755)
            //读取链接
            resp, geterr := http.Get(u)
    
            if geterr != nil || resp.StatusCode != http.StatusOK {
                    //resp.Body.Close()
                    return false
            }
            body, _ := ioutil.ReadAll(resp.Body)
            //fmt.Println(body)
            //创建保存目录
            _, err := os.Stat(path)
            if err != nil {
                    os.MkdirAll(path, 0755)
            }
    
            io.WriteString(f, string(body))
            resp.Body.Close()
            body = nil
            return true
    }
    
    /*
    广度优先算法
    */
    // breadthFirst calls f for each item in the worklist.
    // Any items returned by f are added to the worklist.
    // f is called at most once for each item.
    func breadthFirst(f func(item string) []string, worklist []string) {
            seen := make(map[string]bool)
            for len(worklist) > 0 {
                    items := worklist
                    worklist = nil
                    for _, item := range items {
                            if !seen[item] {
                                    seen[item] = true
                                    worklist = append(worklist, f(item)...)
                            }
                    }
            }
    }
    

      

  • 相关阅读:
    软件测试—— junit 单元测试
    falut error failure 的区别与理解
    错误的反思
    只能在微信浏览器打开的链接,如何查看源码
    PHPManage for IIS Windows 10
    wamp mysql配置
    CSS Flexbox 学习指南、工具与框架
    Android SDK 在线更新镜像服务器资源
    64位win2003 IIS6运行32位的.NET程序
    让服务器iis支持.apk文件下载的设置方法
  • 原文地址:https://www.cnblogs.com/taoshihan/p/8875218.html
Copyright © 2011-2022 走看看