码迷,mamicode.com
首页 > 编程语言 > 详细

go语言实现爬虫采集联想词

时间:2015-02-28 23:10:45      阅读:674      评论:0      收藏:0      [点我收藏+]

标签:

/****************************************************

go语言实现爬虫_联想词 咨询:qq:1465376564

黄哥python培训班所写

python北京周末培训班

https://github.com/pythonpeixun/article/blob/master/beijing_weekend.md

python

上海周末培训班

https://github.com/pythonpeixun/article/blob/master/shanghai_weekend.md


*****************************************************/


package main


import (

"fmt"

"io/ioutil"

"net/http"

"regexp"

)


//定义新的数据类型

type Spider struct {

url    string

header map[string]string

}


//定义 Spider的方法

func (keyword Spider) get_html_header() string {


client := &http.Client{}


req, err := http.NewRequest("GET", keyword.url, nil)

if err != nil {

}


for key, value := range keyword.header {


req.Header.Add(key, value)

}


resp, err := client.Do(req)

if err != nil {

}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)

if err != nil {

}

return string(body)


}


func main() {

header := map[string]string{"Host": "sug.so.360.cn",

"Referer":    "http://www.so.com/",

"DNT":        "1",

"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36",

"Cookie":     "__huid=104rl%2B0HjG2YltBarbPPIz2w7HTbLrv43gETLeVtBdIEI%3D",

}

keyword := "科技"

url := "http://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word,obdata&word=" + keyword


spider := &Spider{url, header}


html := spider.get_html_header()

rp1 := regexp.MustCompile("{(.*?)}")

find_txt := rp1.FindAllString(html, -1)

fmt.Println(find_txt)

}


go语言实现爬虫采集联想词

标签:

原文地址:http://my.oschina.net/pythonpeixun/blog/380908

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!