Go language implements crawler to collect associative words

Source: Internet
Author: User

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

Go Language Implementation Crawler _ Lenovo Word Consulting: qq:1465376564

Written by Huanggo Python training course

Python Beijing Weekend Course

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

Python

Shanghai Weekend Course

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


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


Package Main


Import (

"FMT"

"Io/ioutil"

"Net/http"

"RegExp"

)


Define a new data type

Type Spider struct {

URL string

Header map[string]string

}


Ways to define Spiders

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: = "Technology"

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 language implements crawler to collect associative words

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.