Download Plugin Tool
go get -v github.com/gpmgo/gopm
Automatically import package plug-ins
gopm get -g -v golang.org/x/tools/cmd/goimports
Transcoding Plugin
gopm get -g -v golang.org/x/text
Automatically detect page encodings
gopm get -g -v golang.org/x/net/html
Package Mainimport ("Net/http" "Io/ioutil" "FMT" "Golang.org/x/text/transform" "IO" golang.org/x/text/ Encoding "" Golang.org/x/net/html/charset "" Bufio "" RegExp ") func main () {resp, err: = http. Get ("Http://www.zhenai.com/zhenghun") if err! = Nil {panic (err)} Defer resp. Body.close () If Resp. StatusCode! = http. Statusok {fmt. Println ("Error:status code", resp. StatusCode) return} e: = Determinencoding (resp. Body) Utf8reader: = transform. Newreader (resp. Body, E.newdecoder ()) all, err: = Ioutil. ReadAll (Utf8reader) if err! = Nil {panic (err)} printcitylist (All)}func printcitylist (contents []byte) { Re: = RegExp. Mustcompile (' <a href= ' (http://www.zhenai.com/zhenghun/[0-0a-z]+) [^>]*> ([^<]+) </a> ') Matches: = Re. Findallsubmatch (contents,-1) for _, M: = Range matches {fmt. Printf ("City:%s, URL:%s\n", m[2], m[1])} FMT. Printf ("Matches found:%d\n", Len (Matches))}fuNC determinencoding (r io. Reader) encoding. Encoding {bytes, err: = Bufio. Newreader (R). Peek (1024x768) if err! = Nil {panic (err)} E, _, _: = CharSet. Determineencoding (Bytes, "") return E}