dongyongkui6329 2016-01-25 14:31
浏览 115
已采纳

如何在Golang中有效存储对文件的HTML响应

I'm trying to build a crawler in Golang. I'm using net/http library to download the html file from url. I'm trying to save http.resp and http.Header into file.

How to convert these two file from their respective format into string so that, it could be written to a text file.

I also see a question asked earlier on parsing a stored html response file. Parse HTTP requests and responses from text file in Go . Is there any way to save the url response in this format.

  • 写回答

3条回答 默认 最新

  • dtvq4978 2016-01-25 14:44
    关注

    Edit: Thanks to @JimB for pointing to the http.Response.Write method which makes this a lot easier than I proposed in the beginning:

    resp, err := http.Get("http://google.com/")
    
    if err != nil{
        log.Panic(err)
    }
    
    f, err := os.Create("output.txt")
    defer f.Close()
    
    resp.Write(f)
    

    This was my first Answer

    You could do something like this:

    resp, err := http.Get("http://google.com/")
    
    body, err := ioutil.ReadAll(resp.Body)
    
    // write whole the body
    err = ioutil.WriteFile("body.txt", body, 0644)
    if err != nil {
        panic(err)
    }
    

    This was the edit to my first answer:

    Thanks to @Hector Correa who added the header part. Here is a more comprehensive snippet, targeting your whole question. This writes header followed by the body of the request to output.txt

    //get the response
    resp, err := http.Get("http://google.com/")
    
    //body
    body, err := ioutil.ReadAll(resp.Body)
    
    //header
    var header string
    for h, v := range resp.Header {
        for _, v := range v {
            header += fmt.Sprintf("%s %s 
    ", h, v)
        }
    }
    
    //append all to one slice
    var write []byte
    write = append(write, []byte(header)...)
    write = append(write, body...)
    
    //write it to a file
    err = ioutil.WriteFile("output.txt", write, 0644)
    if err != nil {
        panic(err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么