Created
August 25, 2018 05:25
-
-
Save kkabetani/c8a300e81b85532101c2aa403d0a43c2 to your computer and use it in GitHub Desktop.
Puppeteer を使って Yahoo のニュースをスクレイピング
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
(async () => { | |
// ブラウザ起動 | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
// ヤフーのページを開く | |
await page.goto('https://www.yahoo.co.jp/'); | |
// ヤフーページ内でニュースの一覧を取得する Javascript を実行する | |
const scrapingNews = await page.evaluate(() => { | |
const news = []; | |
const nodes = document.querySelectorAll('.emphasis > li > a'); | |
nodes.forEach(node => { | |
news.push(node.innerText); | |
}) | |
return news; | |
}); | |
console.log(scrapingNews); | |
// ブラウザを閉じる | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment