zoukankan      html  css  js  c++  java
  • go http爬虫

    1 
    
    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    	"net/http"
    )
    
    func main() {
    	resp,err := http.Get("http://yeves.cn")
    	if err != nil{
    		fmt.Print("http get err",err)
    		return
    	}
    
    	body,err := ioutil.ReadAll(resp.Body)
    	if err != nil{
    		fmt.Print("http get err",err)
    		return
    	}
    
    	fmt.Print(string(body))
    }
    
    
    
    
    2
    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    	"net/http"
    )
    
    func main() {
    	url := "http://yeves.cn"
    	download(url)
    }
    
    func download(url string) {
    	client := &http.Client{}
    	req,_ := http.NewRequest("GET",url,nil)
    
    	req.Header.Set("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
    	resp,err := client.Do(req)
    	if err != nil{
    		fmt.Print("http get err",err)
    		return
    	}
    
    	defer resp.Body.Close()
    
    	body,err := ioutil.ReadAll(resp.Body)
    	if err != nil{
    		fmt.Print("read error ",err)
    		return
    	}
    	fmt.Print(string(body))
    }
    
    
    3
    
    package main
    
    import (
    	"fmt"
    	"github.com/jackdanger/collectlinks"
    	"net/http"
    )
    
    func main() {
    	url := "http://www.baidu.com/"
    	download(url)
    }
    
    func download(url string) {
    	client := &http.Client{}
    	req,_ := http.NewRequest("GET",url,nil)
    
    	req.Header.Set("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
    	resp,err := client.Do(req)
    	if err != nil{
    		fmt.Print("http get err",err)
    		return
    	}
    
    	defer resp.Body.Close()
    
    	links := collectlinks.All(resp.Body)
    	for _,link := range links{
    		fmt.Print("parse url",link+"
    ")
    	}
    }
    

      

  • 相关阅读:
    2016年3月iOS面试总结
    iOS常用公共方法
    让你的App说出多国语言——iOS开发之本地化(国际化)
    开发中遇到的坑
    Git简明教程
    iOS-打包成ipa的4种方法
    iOS-最全的App上架教程
    android 开源项目
    android 文件缓存工具类
    android 聊天通讯源码
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13553729.html
Copyright © 2011-2022 走看看