-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeMapScraper.js
66 lines (55 loc) · 2.34 KB
/
treeMapScraper.js
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
const puppeteer = require("puppeteer");
//tree map URL
const url = "https://www.opentreemap.org/phillytreemap/map/";
//Edible trees dataset name list
const edibleTreeList = [
"Allegheny Serviceberry",
"Apple Serviceberry",
"Canadian Serviceberry",
"Downy Serviceberry",
"Serviceberry [Amelanchier spp]",
"Mulberry",
"Red Mulberry",
"White Mulberry",
"Ginkgo",
];
async function run() {
//function runs once for each tree in the edibleTreeList array
for (const tree of edibleTreeList) {
//launches headless browser, visits opentreemap.com
const browser = await puppeteer.launch();
const page = await browser.newPage();
//creates the 'treeOutput' folder within the project folder. All CSV files will populate there
await page._client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath: "treeOutput/",
});
//headless call to URL
await page.goto(url);
//waits for site to load/render
await page.waitForSelector("#boundary-typeahead");
//Types 'Philadelphia' and selects the 'Philadelphia County option from the autopopulated dropdown list
await page.click("#boundary-typeahead");
await page.type("#boundary-typeahead", "Philadelphia");
const locationSuggestionElement = ".tt-dataset-1 > div:nth-child(1)";
await page.waitForSelector(locationSuggestionElement);
await page.click(locationSuggestionElement);
//Types tree species from the array list and selects matching type from autocomplete/suggestion list
await page.waitForSelector("#species-typeahead");
await page.click("#species-typeahead");
await page.type("#species-typeahead", tree);
const treeSuggestionElement = ".tt-dataset-0 > div:nth-child(1)";
await page.waitForSelector(treeSuggestionElement);
await page.click(treeSuggestionElement);
//clicks submit button and creates screenshot rendering for confirmation
await page.click("#perform-search");
await page.waitForNavigation();
await page.screenshot({ path: `${tree}-search.png` });
//selects the download tree information button to create and download CSV
await page.click("a.btn.btn-primary.btn-xs.exportBtn.hidden-xs");
await page.waitForTimeout(15000);//added to allow for downloads before re-rendering page
await page.screenshot({ path: `${tree}-confirmation.png` });
browser.close();
}
}
run();