Skip to content

Instantly share code, notes, and snippets.

@ckhung
Last active December 9, 2020 23:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckhung/e92889b400fb7c2a2e6da8382bacb674 to your computer and use it in GitHub Desktop.
Save ckhung/e92889b400fb7c2a2e6da8382bacb674 to your computer and use it in GitHub Desktop.
puppeteer 簡單互動範例: 下載環保署空氣品質監測網資料
#!/usr/bin/env node
// http://toddhayton.com/2018/08/01/scraping-with-puppeteer/
// airtw-scraper.js https://www.cwb.gov.tw/V7/observe/real/46757.htm > a.htm
const homedir = require('os').homedir();
const puppeteer = require(homedir + '/node_modules/puppeteer');
const url = 'https://airtw.epa.gov.tw/';
async function main() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.waitForSelector('body');
await page.click('#ddl_county')
await page.waitFor(4000)
await page.select('#ddl_county', 'Kaohsiung');
//await page.waitForSelector('#ddl_site');
await page.waitFor(8000)
await page.click('#ddl_site');
await page.waitFor(2000)
await page.select('#ddl_site', '17');
await page.waitFor(2000)
console.log(await page.content());
await browser.close();
}
main();
@NKid
Copy link

NKid commented Feb 27, 2019

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
await page.select('#ddl_county', 'Kaohsiung');
await page.select('#ddl_site', '17'); //左營
await page.waitForResponse('https://airtw.epa.gov.tw/ajax.aspx')
let areaInfo = await page.$('#th_area aside > div');
await areaInfo.screenshot({ path: 'example.png' }); //截圖檢查是否正確
await browser.close();

我用waitForResponse測試起來是可行

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment