dtlab08822 2017-11-11 00:29
浏览 104

如何在Golang中不使用ioutil.ReadAll检索网站源代码

My code:

func getSourceUrl(url string) (string, error) {
   resp, err := http.Get(url)
   if err != nil {
       fmt.Println("Error getSourceUrl: ")
       return "", err
   }
   defer resp.Body.Close()
   body := resp.Body
   // time = 0
   sourcePage, err := ioutil.ReadAll(body)
   // time > 5 minutes
   return string(sourcePage), err
}

I have a website link with a source of around> 100000 lines. Using ioutil.ReadAll made me get very long (about> 5 minutes for 1 link). Is there a way to get Source website faster? Thank you!

  • 写回答

3条回答 默认 最新

  • douhuang3833 2017-11-11 00:43
    关注

    You could iterate sections of the response at a time, something like;

    responseSection := make([]byte, 128)
    body.Read(responseSection)
    return string(responseSection), err
    

    Which would read 128 bytes at a time. However would suggest confirming the download speed is not causing the slow load.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料