-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcanvas.js
More file actions
55 lines (53 loc) · 1.47 KB
/
canvas.js
File metadata and controls
55 lines (53 loc) · 1.47 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
const puppeteer = require("puppeteer");
const fs = require("fs");
class scrapy {
async main() {
this.browser = await puppeteer.launch({
headless: false,
args: ["--proxy-server=127.0.0.1:8090"]
});
this.page = await this.browser.newPage();
await this.page.setViewport({
width: 1242,
height: 575,
deviceScaleFactor: 1
});
await this.page.goto("http://data.eastmoney.com/hsgtcg/");
await this.page.waitForSelector("canvas");
await this.scrool("canvas");
var canvas = await this.page.$("canvas");
for (let i = 0; i < 300; i++) {
var box = await canvas.boundingBox();
await new Promise(resolve => {
setTimeout(resolve, 30);
});
await this.page.mouse.move(
box.x + (i * box.width) / 160,
box.y + box.height / 2
);
try {
await this.page.waitForSelector("#zjlx_chart>div:nth-child(2)", {
timeout: 100
});
} catch {
continue;
}
var text = await this.page.evaluate(() => {
var data = document.querySelector("#zjlx_chart>div:nth-child(2)")
.textContent;
return data;
});
fs.appendFileSync(__dirname + "/" + "test.csv", text + "\n");
}
}
to_json(strings) {}
async scrool(selector) {
var canvas = await this.page.$(selector);
var box = await canvas.boundingBox();
await this.page.evaluate(e => {
window.scrollTo(0, e);
}, box.y);
}
}
a = new scrapy();
a.main();