-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
230 lines (195 loc) · 7.1 KB
/
parser.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gocrawler
import (
"fmt"
"log"
"os"
"encoding/csv"
"io"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/axgle/mahonia"
)
func ScrapeTest() error {
file, err := os.Open("../test/welfare=")
defer file.Close()
if err != nil {
log.Println("open file failed", err.Error())
return err
}
doc, err := goquery.NewDocumentFromReader(file)
if err != nil {
log.Println("newdocumentfromreader failed", err.Error())
return err
}
pTitle := doc.Find("title").Text()
var gbk = mahonia.NewDecoder("gbk")
n := gbk.ConvertString(pTitle)
log.Printf("title:%v\n", n)
// var count = 0
saveFile, err := os.Create("../test/51.csv")
defer saveFile.Close()
if err != nil {
log.Println("open file failed", err.Error())
return err
}
w := csv.NewWriter(saveFile)
//records := make([][]string, 0)
doc.Find(".dw_table").EachWithBreak(func(i int, s *goquery.Selection) bool {
//遍历孩子节点,需要中断跳出,所以用了EachWithBreak
s.Children().EachWithBreak(func(j int, selection *goquery.Selection) bool {
//fmt.Println(selection.Text())
//获取内容
// str := selection.Text()
// fmt.Println(gbk.ConvertString(str))
selection.Find("div.rt").EachWithBreak(func(k int, ss *goquery.Selection) bool {
ss.Find("a").EachWithBreak(func(kk int, sss *goquery.Selection) bool {
href, exist := sss.Find("a.dicon Dm on").Attr("href")
if exist {
fmt.Printf("href=%s\n", href)
}
return true
})
// href, exist := ss.Attr("href")
// if exist {
// fmt.Printf("href=%s\n", href)
// }
return true
})
// title := selection.Find("p.t1").Text()
// company := selection.Find("span.t2").Text()
// location := selection.Find("span.t3").Text()
// salary := selection.Find("span.t4").Text()
// date := selection.Find("span.t5").Text()
// if title != "" || company != "" {
// title = strings.TrimSpace(title)
// count++
// // if count == 1 {
// // return true
// // }
// fmt.Printf("%d:%s\t%s\t%s\t%s\t%s\n", count, gbk.ConvertString(title),
// gbk.ConvertString(company), gbk.ConvertString(location),
// gbk.ConvertString(salary), gbk.ConvertString(date))
// strslice := make([]string, 0)
// strslice = append(strslice, strconv.Itoa(count))
// strslice = append(strslice, gbk.ConvertString(title))
// strslice = append(strslice, gbk.ConvertString(company))
// strslice = append(strslice, gbk.ConvertString(location))
// strslice = append(strslice, gbk.ConvertString(salary))
// strslice = append(strslice, gbk.ConvertString(date))
// //records = append(records, strslice...)
// w.Write(strslice)
// }
//fmt.Println("--------------------------")
return true
})
//fmt.Println("==================")
if i == 0 {
return false
}
return true
})
//w.WriteAll(records)
w.Flush()
log.Println("END")
return nil
}
func (c *Crawler) Scrape51JobDetail(reader io.Reader) error {
doc, err := goquery.NewDocumentFromReader(reader)
if err != nil {
log.Println("newdocumentreader failed", err.Error())
return err
}
var gbk = mahonia.NewDecoder("gbk")
doc.Find("div.tCompany_main").EachWithBreak(func(i int, s *goquery.Selection) bool {
text := s.Find("div.job_msg p").Text()
//fmt.Printf("text : %s\n", strings.Trim(text, "\n"))
fmt.Printf("text : %s\n", gbk.ConvertString(strings.Trim(strings.TrimSpace(text), "\n")))
c.detail = c.detail + gbk.ConvertString(strings.Trim(strings.TrimSpace(text), " ")) + "\n"
// s.Children().EachWithBreak(func(j int, selection *goquery.Selection) bool {
// selection.Find("p").EachWithBreak(func(kk int, sss *goquery.Selection) bool {
// text := sss.Text()
// // fmt.Printf("text : %s\n", gbk.ConvertString(strings.TrimSpace(text)))
// c.detail = c.detail + gbk.ConvertString(strings.TrimSpace(text)) + "\n"
// return true
// })
// return true
// })
return true
})
return nil
}
func (c *Crawler) Scrape51(reader io.Reader) error {
doc, err := goquery.NewDocumentFromReader(reader)
if err != nil {
log.Println("newdocumentfromreader failed", err.Error())
return err
}
//Title
pTitle := doc.Find("title").Text()
var gbk = mahonia.NewDecoder("gbk")
n := gbk.ConvertString(pTitle)
log.Printf("title:%v\n", n)
//Content Jobs list
doc.Find(".dw_table").EachWithBreak(func(i int, s *goquery.Selection) bool {
//遍历孩子节点,需要中断跳出,所以用了EachWithBreak
s.Children().EachWithBreak(func(j int, selection *goquery.Selection) bool {
selection.Find("div.rt").EachWithBreak(func(k int, ss *goquery.Selection) bool {
ss.Find("a").EachWithBreak(func(kk int, sss *goquery.Selection) bool {
href, exist := sss.Attr("href")
id, exist := sss.Attr("id")
if exist {
id = strings.TrimSpace(id)
if 0 == strings.Compare(id, "rtNext") {
c.queryUrl = strings.TrimSpace(href)
fmt.Printf("need query: %s", c.queryUrl)
return true
}
}
return true
})
return true
})
//Find title requirements
requirement, exist := selection.Find("p.t1").Find("a").Attr("href")
if exist {
// fmt.Printf("detail: %s\n", requirement)
// c.RequestWithFunc(requirement, c.Scrape51JobDetail)
}
title := selection.Find("p.t1").Text()
company := selection.Find("span.t2").Text()
location := selection.Find("span.t3").Text()
salary := selection.Find("span.t4").Text()
date := selection.Find("span.t5").Text()
if title != "" || company != "" {
title = strings.TrimSpace(title)
c.line++
strslice := make([]string, 0)
strslice = append(strslice, strconv.Itoa(c.line))
strslice = append(strslice, gbk.ConvertString(title))
strslice = append(strslice, gbk.ConvertString(company))
strslice = append(strslice, gbk.ConvertString(location))
strslice = append(strslice, gbk.ConvertString(salary))
strslice = append(strslice, gbk.ConvertString(date))
strslice = append(strslice, requirement)
strslice = append(strslice, c.detail)
c.detail = ""
c.writer.Write(strslice)
}
return true
})
if i == 0 {
return false
}
return true
})
c.writer.Flush()
log.Println("Parse Finished!!!")
if c.queryUrl != "" {
c.Get(c.queryUrl)
}
return nil
}
//https://search.51job.com/list/040000,000000,0000,00,9,99,golang,2,2.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=
//https://search.51job.com/list/040000,000000,0000,00,9,99,golang,2,1.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=1&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=
//https://search.51job.com/list/040000,000000,0000,00,9,99,golang,2,1.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=