Skip to content

Instantly share code, notes, and snippets.

@kkabetani
Created August 25, 2018 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkabetani/c8a300e81b85532101c2aa403d0a43c2 to your computer and use it in GitHub Desktop.
Save kkabetani/c8a300e81b85532101c2aa403d0a43c2 to your computer and use it in GitHub Desktop.
Puppeteer を使って Yahoo のニュースをスクレイピング
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