上次用Scala寫了個爬蟲。最近在閑工夫之時,學習Go語言,便用Go移植了那個用Scala寫的爬蟲,代碼如下:
package mainimport ("fmt""io/ioutil""net/http""regexp")var (ptnIndexItem = regexp.MustCompile(`<a target="_blank" href="(.+\.html)" title=".+" >(.+)</a>`)ptnContentRough = regexp.MustCompile(`(?s).*<div class="artcontent">(.*)<div id="zhanwei">.*`)ptnBrTag = regexp.MustCompile(`<br>`)ptnHTMLTag = regexp.MustCompile(`(?s)</?.*?>`)ptnSpace = regexp.MustCompile(`(^\s+)|( )`))func Get(url string) (content string, statusCode int) {resp, err1 := http.Get(url)if err1 != nil {statusCode = -100return}defer resp.Body.Close()data, err2 := ioutil.ReadAll(resp.Body)if err2 != nil {statusCode = -200return}statusCode = resp.StatusCodecontent = string(data)return}type IndexItem struct {url stringtitle string}func findIndex(content string) (index []IndexItem, err error) {matches := ptnIndexItem.FindAllStringSubmatch(content, 10000)index = make([]IndexItem, len(matches))for i, item := range matches {index[i] = IndexItem{"http://www.yifan100.com" + item[1], item[2]}}return}func readContent(url string) (content string) {raw, statusCode := Get(url)if statusCode != 200 {fmt.Print("Fail to get the raw data from", url, "\n")return}match := ptnContentRough.FindStringSubmatch(raw)if match != nil {content = match[1]} else {return}content = ptnBrTag.ReplaceAllString(content, "\r\n")content = ptnHTMLTag.ReplaceAllString(content, "")content = ptnSpace.ReplaceAllString(content, "")return}func main() {fmt.Println(`Get index ...`)s, statusCode := Get("http://www.yifan100.com/dir/15136/")if statusCode != 200 {return}index, _ := findIndex(s)fmt.Println(`Get contents and write to file ...`)for _, item := range index {fmt.Printf("Get content %s from %s and write to file.\n", item.title, item.url)fileName := fmt.Sprintf("%s.txt", item.title)content := readContent(item.url)ioutil.WriteFile(fileName, []byte(content), 0644)fmt.Printf("Finish writing to %s.\n", fileName)}}
程式碼數比Scala版的有一定增加,主要原因有以下幾方面原因:
1 golang 重視代碼書寫規範,或者說代碼格式,很多地方寫法比較固定,甚至比較麻煩。比如就算是if判斷為真後的執行語句只有一句話,按照代碼規範,也要寫出帶大括弧的三行,而在Scala和很多其他語言中,一行就行;
2 golang 的strings包和regexp包提供的方法並不特別好用,特別是和Scala相比,使用起來感覺Scala的正則和字串處理要舒服的多;
3 scala版的爬蟲裡面用到了Scala標準庫中的實用類和方法,它們雖然不是文法組成,但用起來感覺像是文法糖,這裡很多方法和函數式編程有關,golang的函數式編程還沒有去仔細學習。
當然golang版的爬蟲也有一個優勢,就是編譯速度很快,執行速度在現在的寫法裡面體現不出優勢;golang的特性goroutine在這裡沒有用到,這段代碼今後會不斷改進。