-
Notifications
You must be signed in to change notification settings - Fork 1
/
summary.go
130 lines (112 loc) · 3.2 KB
/
summary.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
package main
import (
"encoding/csv"
//"fmt"
"log"
"os"
"path/filepath"
"github.com/gocolly/colly"
"strings"
)
var (
donain = "https://app02.szmqs.gov.cn"
start_url = donain + "/0501W/Default.aspx?page=1"
property_url = donain + "/0501W/Iframe/LicItemIframe.aspx?licId="
property_dir = "./property"
)
func main() {
if _, err := os.Stat(property_dir); os.IsNotExist(err) {
os.Mkdir(property_dir, 0777)
}
fName := "sz-house-summary.csv"
f := filepath.Join(property_dir, fName)
file, err := os.Create(f)
if err != nil {
log.Fatalf("Cannot create file %q: %s\n", fName, err)
return
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
// Write CSV header
writer.Write([]string{"company", "property", "location", "area", "id"})
// Instantiate default collector
c := colly.NewCollector(
colly.MaxDepth(15),
colly.Async(true),
)
c.Limit(&colly.LimitRule{DomainGlob: "*", Parallelism: 15})
c.OnHTML("#caTable tbody tr", func(e *colly.HTMLElement) {
itemClass := e.Attr("class")
evenClass := "tab_body bd0"
oddClass := "tab_body bd1"
if strings.EqualFold(itemClass, evenClass) || strings.EqualFold(itemClass, oddClass) {
writer.Write([]string{
e.ChildText("td:nth-child(1)"),
e.ChildText("td:nth-child(2)"),
e.ChildText("td:nth-child(3)"),
e.ChildText("td:nth-child(4)"),
e.ChildAttr("td:nth-child(5) a", "val"),
})
}
go property_crawler(e)
})
c.OnHTML(".xbaiPage_next", func(h *colly.HTMLElement) {
next := donain + h.Attr("href")
log.Printf(next)
c.Visit(next)
})
c.Visit(start_url)
log.Printf("Scraping finished, check file %q for results\n", fName)
c.Wait()
}
// 楼盘划分继续分页爬
func property_crawler(p *colly.HTMLElement) {
name := p.ChildText("td:nth-child(2)")
if name == "" {
return
}
fName := filepath.Join(property_dir, name+".csv")
file, err := os.Create(fName)
if err != nil {
log.Fatalf("Cannot create file %q: %s\n", fName, err)
return
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
// Write CSV header
writer.Write([]string{"房号", "建筑面积", "建筑面积单价", "套内面积", "套内面积单价", "总售价", "销售状态", "备注"})
// Instantiate default collector
c := colly.NewCollector(
colly.MaxDepth(15),
colly.Async(true),
)
c.Limit(&colly.LimitRule{DomainGlob: "*", Parallelism: 15})
c.OnHTML("#caTable tbody tr", func(e *colly.HTMLElement) {
itemClass := e.Attr("class")
evenClass := "tab_body"
oddClass := "tab_body bd1"
if strings.EqualFold(itemClass, evenClass) || strings.EqualFold(itemClass, oddClass) {
writer.Write([]string{
e.ChildText("td:nth-child(1)"),
e.ChildText("td:nth-child(2)"),
e.ChildText("td:nth-child(3)"),
e.ChildText("td:nth-child(4)"),
e.ChildText("td:nth-child(5)"),
e.ChildText("td:nth-child(6)"),
e.ChildText("td:nth-child(7)"),
e.ChildText("td:nth-child(8)"),
})
}
})
c.OnHTML(".xbaiPage_next", func(n *colly.HTMLElement) {
t := donain + n.Attr("href")
log.Printf(t)
c.Visit(t)
})
property_start := property_url + p.ChildAttr("td:nth-child(5) a", "val")
c.Visit(property_start)
c.Wait()
log.Printf("Scraping finished, check file %q for results\n", fName)
}