Skip to content

Instantly share code, notes, and snippets.

@nrm176
Last active November 27, 2018 03:57
Show Gist options
  • Save nrm176/8d26ed33903260df7799a10a2e732aa8 to your computer and use it in GitHub Desktop.
Save nrm176/8d26ed33903260df7799a10a2e732aa8 to your computer and use it in GitHub Desktop.
Yahooトップページのおすすめタブの画像を取得する
const puppeteer = require('puppeteer');
const moment = require('moment');
const FAKE_USERAGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5';
const YAHOO_URL = 'https://www.yahoo.co.jp';
const TARGET_ELEM = ''div#videoFesBoxbd > div#recommendfb > div.lCassette p.thumbnailImg'';

(async () => {

    const browser = await puppeteer.launch({headless: true});
    const page = await browser.newPage();

    //FakeのUserAgentをセットする。
    await page.setUserAgent(FAKE_USERAGENT);
    await page.setViewport({width: 1000, height: 1000, deviceScaleFactor: 2});

  //ヒートマップのURLに行く。waitUntilでページ読み込みが完了するまで待つことができる。
    await page.goto(YAHOO_URL, {waitUntil: 'networkidle2'});
    await page.waitFor(1000);

    //Heatmapのテーブルがある要素を取得
    const element = await page.$(TARGET_ELEM);

    //スクショを撮る
    await element.screenshot({
       path: moment().format('YY-MM-DD') + '_element.png'
    });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment