/
p2p_rong360.go
164 lines (129 loc) · 4.84 KB
/
p2p_rong360.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package crawlers
import (
"finprocess/models"
"github.com/PuerkitoBio/goquery"
"github.com/astaxie/beego"
"github.com/henrylee2cn/mahonia"
"net/http"
"strconv"
"strings"
"time"
)
var client = &http.Client{}
func Rong360Crawler() {
beego.Info("Process rong360 product")
//记录任务执行时的时间
startTime := time.Now()
//只获取前30页的,防止被屏蔽
for i := 1; i < 30; i++ {
u := "http://www.rong360.com/licai-p2p/list/p" + strconv.Itoa(i)
beego.Info("Process product url:" + u)
document := getDocument(u)
if document == nil {
continue
}
document.Find("div.floor.clearfix").Find("table.wd-pro-list").Find("tbody").Find("tr").Each(func(i int, selection *goquery.Selection) {
if i%2 == 0 {
if clickurl, f := selection.Attr("click-url"); f {
invest := &models.Invest_Contract{}
//platform_id
us := strings.Split(clickurl, "-")
if len(us) > 0 {
platform_id := us[len(us)-1]
invest.Platform_id = platform_id
}
var description string
if s := selection.Find("td.td7"); s != nil {
description = strings.TrimSpace(s.Text())
}
if product := getDocument(clickurl); product != nil {
master := &models.Master{}
masterName := product.Find("div.t.clearfix").Find("div.r").Find("p").First().Text()
master.Name = masterName
if icon_url, exists := product.Find("div.t.clearfix").Find("div.l").Find("img").Attr("src"); exists {
master.Platform_icon_url = icon_url
}
if u, exists := product.Find("div.view-r-des").Find("a.btn").Attr("href"); exists {
if website := getDocument(u); website != nil {
if u, f := website.Find("div.m.wrap-left").Find("div.p1").Find("a").Attr("href"); f {
master.Official_url = u
}
}
}
//save master
(&models.MasterDao{}).SaveOrUpdate(master)
//title description
invest.Description = description
invest.For_register = models.For_Register
invest.Amount_max = 999999999
title := product.Find("div.t").Find("span").Text()
invest.Name = title
//product_url
if product_url, exists := product.Find("div.c.clearfix").Find("td.td3").Find("a").Attr("href"); exists {
invest.Product_url = product_url
}
//rate and duration
rateAndDuration := product.Find("div.d").Find("p").Nodes
if len(rateAndDuration) == 3 {
rate_text := goquery.NewDocumentFromNode(rateAndDuration[0]).Find("span").Text()
if rate, err := strconv.ParseFloat(strings.TrimRight(rate_text, "%"), 16); err == nil {
invest.Rate = rate
}
durationNode := goquery.NewDocumentFromNode(rateAndDuration[1])
if duration, err := strconv.Atoi(durationNode.Find("span").Text()); err == nil {
invest.Duration_min = duration
}
durationTypeText := durationNode.Text()
if strings.Contains(durationTypeText, "天") {
invest.Duration_type = models.Duration_Type_Day
} else if strings.Contains(durationTypeText, "月") {
invest.Duration_type = models.Duration_Type_Month
} else if strings.Contains(durationTypeText, "年") {
invest.Duration_type = models.Duration_Type_Year
}
}
clearfix := product.Find("div.c.clearfix").Find("table.tab1").Find("tr").Nodes
if len(clearfix) == 6 {
amountNode := goquery.NewDocumentFromNode(clearfix[3])
if amount, err := strconv.Atoi(strings.TrimLeft(strings.TrimRight(amountNode.Find(".td2").Text(), "万元"), "元")); err == nil {
invest.Amount_min = amount
}
}
//save invest_contract
masterId := (&models.MasterDao{}).Query(masterName)
invest.Master_id = masterId
(&models.Invest_ContractDao{}).SaveOrUpdate(invest)
}
}
}
})
}
//删除之前的数据
investDao := &models.Invest_ContractDao{}
investDao.DeleteAllRong360(startTime)
}
func getDocument(url string) *goquery.Document {
req, err := http.NewRequest("GET", url, nil)
if err == nil {
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
// req.Header.Add("Accept-Encoding", "gzip, deflate, sdch") //有这个会乱码
req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6")
req.Header.Add("Cache-Control", "max-age=0")
req.Header.Add("Connection", "keep-alive")
req.Header.Add("Host", "www.rong360.com")
req.Header.Add("Upgrade-Insecure-Requests", "1")
req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2490.71 Safari/537.36")
resp, err := client.Do(req)
if err == nil && resp.Body != nil {
decoder := mahonia.NewDecoder("UTF-8")
document, err := goquery.NewDocumentFromReader(decoder.NewReader(resp.Body))
if err == nil {
return document
}
}
}
if err != nil {
beego.Error(err)
}
return nil
}