forked from haojiwu/line-bot-nodejs-on-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oilNew.js
40 lines (35 loc) · 1.02 KB
/
oilNew.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
const puppeteer = require("puppeteer");
// process.env.PUPPETEER_CHROMEDRIVER_PATH = "\node_modules\chromedriver\lib\chromedriver";
async function getOil() {
process.chdir(__dirname);
const browser = await puppeteer.launch({
headless: "new",
// headless: false,
});
const page = await browser.newPage();
await page.goto("https://www.cpc.com.tw/");
const [link] = await page.$x('//a[contains(text(), "更多新聞稿")]');
await Promise.all([
link.click(),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);
// 撈取標題
const keyword = "平穩雙機制";
const newsCaptions = await page.$$(".caption");
let item = [];
let count = 0;
for (let newsCaption of newsCaptions) {
const text = await newsCaption.evaluate((el) => el.textContent);
if (text.includes(keyword)) {
//console.log(text + "\n");
item.push(text);
count++;
if (count === 3) break;
}
}
console.log(item);
await browser.close();
return item;
}
getOil();
module.exports = getOil;