1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DenoでPuppeteer動かしてみた

Posted at

Denoの存在は知っていたけど特に触る機会もなかったので放置していました
一応インストールだけはしていた

ちょっと調べてみたらDenoでTypeScriptがそのまま動かせることや、Puppeteerも用意されているとのことだったのでTypeScriptでPuppeteerを動かす最小限のスクリプトを書いてみました

ちなみにインストールは以下のコマンドでOK

curl -fsSL https://deno.land/x/install/install.sh | sh

インストールしたのがちょっと前だったのでまずはアップグレードしてみました

deno upgrade
Looking up latest version
Found latest version 1.29.4
Downloading https://github.com/denoland/deno/releases/download/v1.29.4/deno-x86_64-unknown-linux-gnu.zip
Deno is upgrading to version 1.29.4
Archive:  /tmp/.tmpB2ijqw/deno.zip
  inflating: deno
Upgraded successfully

Denoのインストールもバージョンアップも簡単すぎる!

ひじょうにかんたんなコードを用意する

index.ts
import puppeteer from "https://deno.land/x/puppeteer/mod.ts";

const browser: object = await puppeteer.launch();
const page: object = await browser.newPage();
await page.goto("https://www.google.com/");
await page.screenshot({path: "/dev/shm/deno-puppeteer-sample.png"});

await browser.close();

実行してみる

deno run -A --unstable index.ts

おや?エラーが出るぞ?

Warning Implicitly using latest version (16.2.0) for https://deno.land/x/puppeteer/mod.ts
error: Uncaught Error: Could not find browser revision 1022525. Run "PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts" to download a supported browser binary.
      if (missingText) throw new Error(missingText);
                             ^
    at ChromeLauncher.launch (https://deno.land/x/puppeteer@16.2.0/src/deno/Launcher.ts:99:30)
    at async file:///home/sakura/src/my_projects/typescript/pairs/index.ts:3:25

Run "PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts" とあるのでこれを実行してみます

PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts

ちょっと時間がかかりましたが、終わったので再度実行してみます

deno run -A --unstable index.ts

無事に終了し、指定した場所にスクショが生成されていました!

結果

deno-puppeteer-sample.png

非常に簡単にTypeScript + Puppeteerを使えそうです

今後スクレイピングとかするときのスクリプトはDenoで動かそうと思います


それでは良いDenoライフを!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?