根據(jù)前面學(xué)習(xí)的爬蟲方面的知識,自己最中國國旅網(wǎng)頁進(jìn)行了爬蟲,現(xiàn)將代碼分享如下:
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"strconv"
"strings"
)
//http://www.szcits.cn/line/346/
func main() {
list := PaseGuoLv()
if len(list) == 0 {
fmt.Println("獲取旅游城市失敗")
} else {
fmt.Println("獲取旅游城市成功" + strconv.Itoa(len(list)))
}
}
type TravelModel struct {
title string //標(biāo)題
sub_tiltle string //子標(biāo)題
set_out_city string //出發(fā)城市
price int //價格
per_good int //滿意度
look_count int //瀏覽次數(shù)
detail_url string //詳情的ulr
dateString string //出發(fā)時間
price_type string //幣種$催享,¥
}
func PaseGuoLv() []TravelModel {
list := make([]TravelModel, 1)
fmt.Println("開始爬蟲中國國旅的數(shù)據(jù)...")
js, err := goquery.NewDocument("http://www.szcits.cn/line/346/")
if err != nil {
fmt.Println("NewDocument Error")
log.Fatal(err)
} else {
fmt.Println("NewDocument right")
js.Find(".big_box section").Each(func(i int, contentionSection *goquery.Selection) {
model := TravelModel{}
intro := contentionSection.Find(".intro .sub_title").Text()
city := contentionSection.Find(".city_title span").Text()
title := contentionSection.Find(".intro h3 a").Text()
price := contentionSection.Find(".right .price strong").Text()
price_type := contentionSection.Find(".right .price span").Text()
like_look := contentionSection.Find(".right .txt span").Text()
html_url, _ := contentionSection.Find(".right .buttom a").Attr("href")
like_Array := strings.Split(like_look, "%")
var like, look string
if len(like_Array) > 1 {
like = like_Array[0]
look = like_Array[1]
}
//date := contentionSection.Find("")
fmt.Println("---------------------")
fmt.Println("\n獲取的旅游介紹是" + intro + "\n\t")
fmt.Println("出發(fā)的城市是:" + strconv.Itoa(i) + "\n\t")
fmt.Println("title is:", title)
fmt.Println("價錢是 is", price_type+price)
fmt.Println("喜歡的數(shù)量是", like+"--"+look)
fmt.Println("html的url是", "http://www.szcits.cn"+html_url)
fmt.Println("---------------------")
price_int, _ := strconv.Atoi(price)
good_int, _ := strconv.Atoi(like)
looK_int, _ := strconv.Atoi(look)
model.sub_tiltle = intro
model.set_out_city = city
model.title = title
model.price_type = price_type
model.price = price_int
model.per_good = good_int
model.look_count = looK_int
model.detail_url = "http://www.szcits.cn" + html_url
list = append(list, model)
})
}
return list
}
未完待續(xù)