-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetChinaCode.js
More file actions
185 lines (137 loc) · 3.93 KB
/
Copy pathgetChinaCode.js
File metadata and controls
185 lines (137 loc) · 3.93 KB
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
const devices = require('puppeteer/DeviceDescriptors')
const puppeteer = require('puppeteer'),
fs = require('fs'),
$ = require('jquery'),
path = require('path');
const XLSX = require("xlsx");
const WorkBook = require("./workbook");
(async () => {
const browser = await puppeteer.launch({
executablePath: '/opt/google/chrome/chrome',
headless: false,
});
let getOnePage = async (url, maxPage) => {
const page = await browser.newPage();
await page.emulate(devices['iPhone X'])
await page.goto(url, {
waitUntil: 'networkidle2'
});
await page.setJavaScriptEnabled(true);
await page.waitFor(1200);
let arr = await page.evaluate(() => {
let trs = document.querySelectorAll('tbody tr')
console.log(trs)
let json = {}
let arr = []
trs.forEach(item => {
if(item.clientHeight == 19){
let text = item.innerText;
/^(\s+)\w+/.test(text)
text.replace(RegExp.$1, '')
let textArr = text.split(' ')
json[textArr[1]] = textArr[2]
arr.push([textArr[1], textArr[2]])
}
})
return arr;
});
console.log(arr)
let chinaMap = {}
let china = []
let top = /^\s{0}/
let sec = /^\s{1}/
let thr = /^\s{3}/
let topStack = []
let secStack = []
let thrStack = []
let isZxs = false
arr.map((item, index)=>{
let code = item[0] || ''
let name = item[1] || ''
chinaMap[code] = name.replace(/\s/g, '')
let next = arr[index + 1]
if(next){
//处理直辖市
if((top.test(name) && !sec.test(name)) && thr.test(next[1])){
isZxs = true
}
}
if(top.test(name) && !sec.test(name)){
topStack.push({code, name: name.replace(/\s/g, '')})
// return false
}
if(sec.test(name) && !thr.test(name)){
secStack.push({code, name: name.replace(/\s/g, '')})
}
if(thr.test(name) && !isZxs){
thrStack.push({code, name: name.replace(/\s/g, '')})
}
if(thr.test(name) && isZxs){
secStack.push({code, name: name.replace(/\s/g, '')})
}
if(next){
//处理直辖市
let len = name.match(/\s/g) ? name.match(/\s/g).length : 0
let lenNext = next[1].match(/\s/g) ? next[1].match(/\s/g).length : 0
//类型变化
if(len > lenNext) {
//市变化
if(lenNext == 1 || lenNext == 0){
let last = secStack.pop()
last && (last['children'] = thrStack)
secStack.push(last)
thrStack = []
}
//省变化
if(lenNext == 0){
let last = topStack.pop()
// console.log('lll',last)
last && (last['children'] = secStack)
topStack.push(last)
// if(isZxs){
secStack = []
// }
}
}
if(!sec.test(next[1]) && top.test(next[1])){
isZxs = false
}
//结束
} else {
china = topStack
}
})
// console.log((china))
fs.writeFile('china.json', JSON.stringify(china), function(err) {
if (err) {
return console.error(err);
}
console.log("数据写入成功!");
});
fs.writeFile('chinaMap.json', JSON.stringify(chinaMap), function(err) {
if (err) {
return console.error(err);
}
console.log("数据写入成功!");
});
page.once('load', () => console.log('Page loaded!'));
await page.close()
url.match(/&p=(\w*)$/)
if (RegExp.$1 == maxPage){
await setTimeout(()=>{
browser.close();
},5000)
}
};
(async () => {
let getAllPage = (i, maxPage) => {
console.log('抓取进度:', i + '/' + maxPage)
getOnePage(
"http://www.mca.gov.cn/article/sj/xzqh/2019/2019/201911250933.html",
maxPage
);
}
getAllPage(1, 1)
console.log('hehe')
})()
})()