第10週

2023年 メディアアート・プログラミング 第10回 #

スライド #

puppeteerのコードサンプル #

autoscroll.js4_auto_scroll.jsでライブラリとして使用するだけなので、内容を理解しなくても大丈夫です。

1_launch.js

DL
const puppeteer = require('puppeteer');

const main = async () => {
    let browser = await puppeteer.launch({ headless: false });
    let page = await browser.newPage();
    await page.goto('https://www.youtube.com/');
    await page.screenshot({ path: "./ss.png", fullPage: true });
    await page.close();
    await browser.close();
};

main();

2_async_intro.js

DL
const puppeteer = require('puppeteer');

const main = async () =>
    puppeteer.launch({ headless: false })
        .then(browser => browser.newPage()
            .then(page => page.goto('https://www.youtube.com/')
                .then(_response => page.screenshot({ path: "./ss.png" })
                    .then(_ => page.close()
                        .then(_ => browser.close())))));

main();

3_map_async.js

DL
const puppeteer = require('puppeteer');

const take_screen_shot = async (browser, url, index) => {
    let page = await browser.newPage();
    let path = `${index}.png`;
    await page.goto(url);
    await page.screenshot({ path: path });
    await page.close();
}

const main = async () => {
    let browser = await puppeteer.launch({ headless: false ,args: ["--window-size=1920,1080"],timeout:0});
    const urllist = ["https://www.youtube.com",
        "https://www.google.com",
        "https://www.geidai.ac.jp",
        "https://yahoo.co.jp"]
    await Promise.all(urllist.map((url, index) => take_screen_shot(browser, url, index)));
    await browser.close();
}

main();

4_auto_scroll.js

DL
const puppeteer = require('puppeteer');
const autoScroll = require("./autoscroll.js").autoScroll;

const main = async () => {
    let browser = await puppeteer.launch({ headless: false });
    let page = await browser.newPage();
    await page.goto('https://www.youtube.com/');
    await autoScroll(page,10);
    await page.screenshot({ path: "ss_full.png", fullPage: true })
    await page.close();
    await browser.close();
}
main();

5_youtube_hopper.js

DL
const puppeteer = require('puppeteer');
const autoScroll = require("./autoscroll.js").autoScroll;

const main = async () => {
    let browser = await puppeteer.launch({ headless: false });
    let page = await browser.newPage();
    await page.goto('https://www.youtube.com/');
    for (let i = 0; i < 1000; i++) {
        const urllist = await page.$$eval("a#thumbnail", elems => elems.map(elem => elem.href));
        const url = urllist[3];
        console.log(url)
        await page.goto(url);
        await autoScroll(page, 10);
        await page.screenshot({ path: `${i}.png`, fullPage: true });
    }
    await page.close();
    await browser.close();
}
main();

autoscroll.js

DL
async function autoScroll(page, maxScrolls) {
    await page.evaluate(async maxScrolls => {
        await new Promise((then) => {
            let distance = 1000;
            let scrolls = 0; 
            let timer = setInterval(() => {
                window.scrollBy(0, distance);
                scrolls += 1;
               if (scrolls >= maxScrolls) {
                    clearInterval(timer);
                    then();
                }
            }, 100);
        });
    }, maxScrolls);
}

exports.autoScroll = autoScroll;