yysaki
1/5/2020 - 8:00 AM

booklog読書目標の取得

booklog読書目標の取得

import puppeteer from 'puppeteer';
const url = 'https://booklog.jp/users/{userName}/goal/{id}';

async function main() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url, { waitUntil: 'networkidle0' });

  const rows = await page.evaluate(() => {
    const ret = Array.from(document.querySelectorAll('table.tableType2 tbody tr'))
      .map(tr => {
        return {
          title: tr!.querySelector('a.titleLink')!.innerHTML,
          readAt: tr!.querySelector('em')!.innerHTML,
        };
      });
    return ret;
  });

  await browser.close();

  rows
    .sort((a, b) => Date.parse(a.readAt) - Date.parse(b.readAt))
    .forEach(row => console.log(`${row.readAt} ${row.title}`));
};

main();