{"id":130,"date":"2020-12-19T17:15:37","date_gmt":"2020-12-19T08:15:37","guid":{"rendered":"http:\/\/localhost:8000\/?p=130"},"modified":"2021-04-16T09:16:14","modified_gmt":"2021-04-16T00:16:14","slug":"puppeteer-file-download","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/12\/puppeteer-file-download.html","title":{"rendered":"puppeteer\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9"},"content":{"rendered":"
\u3053\u306e\u8a18\u4e8b\u306f Node.js Advent Calendar 2020<\/a> \u306e\uff12\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u3002<\/strong><\/p>\n puppeteer<\/a> \u3067\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5\u306f\u3053\u3061\u3089\u306e issue<\/a> \u304c\u307e\u3060open\u306a\u3053\u3068\u304b\u3089\u3082\u5206\u304b\u308b\u3088\u3046\u306b\u3001\u3059\u3093\u306a\u308a\u5b9f\u73fe\u3067\u304d\u308b\u516c\u5f0f\u306e\u65b9\u6cd5\u306f\u306a\u3055\u305d\u3046\u3067\u3059\u3002 \u3068\u308a\u3042\u3048\u305a\u3001puppeteer\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002puppeteer\u4ee5\u5916\u306b\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u3001\u500b\u5225\u306b\u8ffd\u8a18\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n \u30b5\u30f3\u30d7\u30eb\u30bd\u30fc\u30b9\u3092\u9069\u5f53\u306a\u540d\u524d\u306e\u30d5\u30a1\u30a4\u30eb\uff08\u305f\u3068\u3048\u3070sample.js\uff09\u306b\u30b3\u30d4\u30da\u3057\u3066node\u30b3\u30de\u30f3\u30c9\u3067\u5b9f\u884c\u3059\u308b\u3060\u3051\u3067\u3001\u5b9f\u969b\u306b\u52d5\u304b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n Chromium\u306ePage.setDownloadBehavior<\/a>\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3057\u3066\u3001\u30d6\u30e9\u30a6\u30b6\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u51e6\u7406\u3092\u8a31\u53ef\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002 \u3053\u3061\u3089<\/a>\u306b\u8a18\u8f09\u3055\u308c\u3066\u3044\u308b\u65b9\u6cd5\u3092\u53c2\u8003\u306b\u3001\u65b9\u6cd51\u3092\u6539\u826f\u3057\u3066\u3044\u307e\u3059\u3002 Page.setDownloadBehavior\u304cdeprecated\u3067\u3044\u3064\u524a\u9664\u3055\u308c\u308b\u304b\u5206\u304b\u3089\u306a\u3044\uff08\u3053\u308c\u306f\u5bfe\u5fdc\u65b9\u6cd5\u304c\u4e0d\u660e\uff09<\/p>\n \u3053\u3061\u3089<\/a>\u3092\u53c2\u8003\u306b\u3001\u5b9f\u969b\u306b\u52d5\u304f\u3088\u3046\u306b\u66f8\u304d\u52a0\u3048\u305f\u3082\u306e\u306b\u306a\u308a\u307e\u3059\u3002 \u691c\u7d22\u3059\u308b\u3068\u6700\u521d\u306b\u30d2\u30c3\u30c8\u3059\u308b stackoverflow<\/a> \u3067\u7d39\u4ecb\u3055\u308c\u3066\u3044\u305f\u65b9\u6cd5\u3082\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \u3053\u306e\u307b\u304b\u3001 \u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3068\u306f\u7570\u306a\u308a\u3001\u958b\u3044\u305fWeb\u30da\u30fc\u30b8\u306e\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8\u3092\u53d6\u308b\u306e\u306f\u3068\u3066\u3082\u7c21\u5358\u3067\u3059\u3002<\/p>\n \u540c\u69d8\u306b\u3001\u958b\u3044\u305fWeb\u30da\u30fc\u30b8\u3092PDF\u3068\u3057\u3066\u4fdd\u5b58\u3059\u308b\u306e\u3082\u3068\u3066\u3082\u7c21\u5358\u3067\u3059\u3002 puppeteer \u306e\u3088\u3046\u306a\u30e1\u30b8\u30e3\u30fc\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3082\u3001\u610f\u5916\u306b\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306e\u65b9\u6cd5\u304c\u307e\u3068\u307e\u3063\u305f\u8a18\u4e8b\u304c\u306a\u304f\u3001\u8272\u3005\u3068\u8a66\u884c\u932f\u8aa4\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \u691c\u8a3c\u306b\u4f7f\u3063\u305f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306b\u7f6e\u3044\u3066\u3042\u308a\u307e\u3059\u3002 \u3053\u306e\u8a18\u4e8b\u306f Node.js Advent Calendar 2020 \u306e\uff12\u65e5\u76ee\u306e\u8a18\u4e8b\u3067\u3059\u3002 puppeteer \u3067\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5\u306f\u3053\u3061\u3089\u306e issue \u304c\u307e\u3060open\u306a\u3053\u3068\u304b\u3089\u3082\u5206\u304b\u308b\u3088\u3046\u306b\u3001\u3059\u3093\u306a\u308a\u5b9f\u73fe\u3067\u304d\u308b\u516c\u5f0f\u306e\u65b9\u6cd5\u306f\u306a\u3055\u305d\u3046\u3067\u3059\u3002 \u3068\u306f\u3044\u3063\u3066\u3082\u3001\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3092\u3057\u3066\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u3044\u30b1\u30fc\u30b9\u306f\u3042\u308b\u308f\u3051\u3067\u3001\u5b9f\u969b\u306b\u8a66\u3057\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002 \u524d\u6e96\u5099 \u3068\u308a\u3042\u3048\u305a\u3001puppeteer\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002puppeteer\u4ee5\u5916\u306b\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u3001\u500b\u5225\u306b\u8ffd\u8a18\u3057\u3066\u3044\u307e\u3059\u3002 npm install puppeteer –save \u5b9f\u884c\u65b9\u6cd5 \u30b5\u30f3\u30d7\u30eb\u30bd\u30fc\u30b9\u3092\u9069\u5f53\u306a\u540d\u524d\u306e\u30d5\u30a1\u30a4\u30eb\uff08\u305f\u3068\u3048\u3070sample.js\uff09\u306b\u30b3\u30d4\u30da\u3057\u3066node\u30b3\u30de\u30f3\u30c9\u3067\u5b9f\u884c\u3059\u308b\u3060\u3051\u3067\u3001\u5b9f\u969b\u306b\u52d5\u304b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 node sample.js \u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b \u65b9\u6cd51\u3010\u25b3\u3011: \u5358\u7d14\u306b\u30d6\u30e9\u30a6\u30b6\u306e\u30d5\u30a1 <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,9],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/130"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":131,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/130\/revisions\/131"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\u3068\u306f\u3044\u3063\u3066\u3082\u3001\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3092\u3057\u3066\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u3044\u30b1\u30fc\u30b9\u306f\u3042\u308b\u308f\u3051\u3067\u3001\u5b9f\u969b\u306b\u8a66\u3057\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\u524d\u6e96\u5099<\/h2>\n
npm install puppeteer --save<\/code><\/pre>\n
\u5b9f\u884c\u65b9\u6cd5<\/h2>\n
node sample.js<\/code><\/pre>\n
\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b<\/h2>\n
\u65b9\u6cd51\u3010\u25b3\u3011: \u5358\u7d14\u306b\u30d6\u30e9\u30a6\u30b6\u306e\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u51e6\u7406\u3092\u5b9f\u884c<\/h3>\n
\n\u6982\u306d\u3046\u307e\u304f\u52d5\u304d\u307e\u3059\u304c\u3001\u30d6\u30e9\u30a6\u30b6\u306e\u30d3\u30e5\u30fc\u30a2\u30fc\u3067\u8868\u793a\u3059\u308b\u30b1\u30fc\u30b9\u306f\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3067\u304d\u307e\u305b\u3093\u3002<\/u><\/p>\nconst puppeteer = require('puppeteer');\n\n(async () => {\n async function sleep(msec) {\n setTimeout(() => { }, msec);\n }\n async function download(pageUrl, selector) {\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n await page.goto(pageUrl);\n client = await page.target().createCDPSession();\n client.send('Page.setDownloadBehavior', {\n behavior: 'allow', \/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u8a31\u53ef\n downloadPath: 'downloads', \/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u5148\u306e\u30d5\u30a9\u30eb\u30c0\u3092\u6307\u5b9a\n });\n await page.click(selector);\n await sleep(5000); \/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u5b8c\u4e86\u3092\u5f85\u3064\n await browser.close();\n }\n await download('https:\/\/www.soumu.go.jp\/toukei_toukatsu\/index\/seido\/9-5.htm', 'a[href*=".csv"]'); \/\/ OK\n await download('https:\/\/www.soumu.go.jp\/toukei_toukatsu\/index\/seido\/9-5.htm', 'a[href*=".pdf"]'); \/\/ OK\n await download('https:\/\/pdf-xml-download-test.vercel.app\/', '#link-pdf'); \/\/ OK\n await download('https:\/\/pdf-xml-download-test.vercel.app\/', '#link-xml'); \/\/ NG\n})();<\/code><\/pre>\n
\u65b9\u6cd52\u3010\u25cb\u3011: \u5f37\u5236\u7684\u306b\u30d6\u30e9\u30a6\u30b6\u306e\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u51e6\u7406\u3092\u5b9f\u884c<\/h3>\n
\nFetch<\/a>\u3092\u5229\u7528\u3057\u3066\u3001xml\u3084pdf\u306e\u5834\u5408\u306fResponseHeader\u306bcontent-disposition: attachment<\/code>\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u7121\u7406\u3084\u308a\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\u65b9\u6cd5\u3067\u3059\u3002
\n\u3053\u306e\u65b9\u6cd5\u306f\u3044\u304f\u3064\u304b\u306e\u30b5\u30a4\u30c8\u3067\u8a66\u3057\u3066\u898b\u305f\u3068\u3053\u308d\u554f\u984c\u306a\u304f\u52d5\u3044\u3066\u304f\u308c\u307e\u3057\u305f\u304c\u3001\uff12\u70b9\u307b\u3069\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n
const puppeteer = require('puppeteer');\n\n(async () => {\n async function sleep(msec) {\n setTimeout(() => { }, msec);\n }\n async function download(pageUrl, selector) {\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n await page.goto(pageUrl);\n client = await page.target().createCDPSession();\n client.send('Page.setDownloadBehavior', {\n behavior: 'allow', \/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u8a31\u53ef\n downloadPath: 'downloads', \/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u5148\u306e\u30d5\u30a9\u30eb\u30c0\u3092\u6307\u5b9a\n });\n\n await client.send('Fetch.enable', { \/\/ Fetch\u3092\u6709\u52b9\u306b\n patterns: [{ urlPattern: '*', requestStage: 'Response' }] \/\/ Response\u30b9\u30c6\u30fc\u30b8\u3092Fetch\n });\n\n await client.on('Fetch.requestPaused', async (requestEvent) => { \/\/ \u3053\u3053\u3067\u8981\u6c42\u3092\u4e00\u6642\u505c\u6b62\n const { requestId } = requestEvent;\n let responseHeaders = requestEvent.responseHeaders || [];\n let contentType = responseHeaders.filter(\n header => header.name.toLowerCase() === 'content-type')[0].value;\n\n \/\/ pdf\u3068xml\u4ee5\u5916\u306f\u305d\u306e\u307e\u307e\n if (!contentType.endsWith('pdf') && !contentType.endsWith('xml')) {\n await client.send('Fetch.continueRequest', { requestId }); \/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u7d9a\u884c\n return;\n }\n\n \/\/ pdf\u3068xml\u306e\u5834\u5408\u306f`content-disposition: attachment`\u3092\u3064\u3051\u308b\n responseHeaders.push({ name: 'content-disposition', value: 'attachment' });\n const response = await client.send('Fetch.getResponseBody', { requestId }); \/\/ body\u3092\u53d6\u5f97\n await client.send('Fetch.fulfillRequest', \/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u6307\u5b9a\n { requestId, responseCode: 200, responseHeaders, body: response.body });\n });\n\n await page.click(selector);\n await sleep(5000); \/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u5b8c\u4e86\u3092\u5f85\u3064\n await browser.close();\n }\n await download('https:\/\/www.soumu.go.jp\/toukei_toukatsu\/index\/seido\/9-5.htm', 'a[href*=".csv"]'); \/\/ OK\n await download('https:\/\/www.soumu.go.jp\/toukei_toukatsu\/index\/seido\/9-5.htm', 'a[href*=".pdf"]'); \/\/ OK\n await download('https:\/\/pdf-xml-download-test.vercel.app\/', '#link-pdf'); \/\/ OK\n await download('https:\/\/pdf-xml-download-test.vercel.app\/', '#link-xml'); \/\/ OK\n})();<\/code><\/pre>\n<\/li>\n<\/ul>\n
\u65b9\u6cd53\u3010\u25ce\u3011: \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306erequest\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u3066\u3001\u540c\u3058\u5185\u5bb9\u3067\u5225\u9014request\u3092\u9001\u4fe1<\/h3>\n
\n\u30ea\u30af\u30a8\u30b9\u30c8\u30a4\u30d9\u30f3\u30c8\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u3066\u3001\u53d6\u5f97\u3057\u305f\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u5185\u5bb9\u3067chromium\u95a2\u4fc2\u306a\u304frequest-promise<\/code>\u3092\u4f7f\u3063\u3066\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002cookie\u306b\u3082\u5bfe\u5fdc\u3057\u3066\u304a\u308a\u3001\u601d\u3044\u3064\u304f\u7bc4\u56f2\u3067\u3046\u307e\u304f\u52d5\u3044\u3066\u304f\u308c\u308b\u6c17\u304c\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u3059\u3067\u306b\u308f\u304b\u3063\u3066\u3044\u308b\u554f\u984c\u70b9\u3082\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n
npm install request request-promise uuid mime-types --save<\/code><\/pre>\n
const puppeteer = require('puppeteer');\nconst rp = require('request-promise');\nconst fs = require('fs');\nconst { v4: uuidv4 } = require('uuid');\nconst mime = require('mime-types');\n\n(async () => {\n \/\/ \u5bfe\u8c61\u30da\u30fc\u30b8\u5185\u306e\u5bfe\u8c61selector\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\n async function download(pageUrl, selector) {\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n await page.goto(pageUrl);\n await page.setRequestInterception(true); \/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30a4\u30f3\u30bf\u30fc\u30bb\u30d7\u30c8\n page.on('request', async (request) => {\n await sendRequest(request, await page.cookies()); \/\/ \u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u305f\u5185\u5bb9\u3067\u5225\u9014\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\n request.abort(); \/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u7d42\u4e86\n });\n await page.click(selector);\n await browser.close();\n }\n\n \/\/ request-promise\u3092\u4f7f\u3063\u3066\u5225\u9014\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\n async function sendRequest(request, cookies) {\n const options = {\n encoding: null,\n method: request._method,\n uri: request._url,\n body: request._postData,\n headers: request._headers,\n resolveWithFullResponse: true, \/\/ content-type\u3092\u53d6\u5f97\u3057\u305f\u3044\u306e\u3067\u3001FullResponse\u3092\u53d6\u5f97\u3059\u308b\u8a2d\u5b9a\n }\n options.headers.Cookie = cookies.map(cookie => cookie.name + '=' + cookie.value).join(';');\n const response = await rp(options);\n const outputPath = `downloads\/${uuidv4()}.${mime.extension(response.headers['content-type'])}`; \/\/ content-type\u304b\u3089\u62e1\u5f35\u5b50\u3092\u5224\u5b9a\u3057\u3001\u51fa\u529b\u30d1\u30b9\u3092\u751f\u6210\n fs.writeFile(outputPath, response.body, () => { }); \/\/ \u30d5\u30a1\u30a4\u30eb\u306b\u51fa\u529b\n }\n\n await download('https:\/\/www.soumu.go.jp\/toukei_toukatsu\/index\/seido\/9-5.htm', 'a[href*=".csv"]'); \/\/ OK\n await download('https:\/\/www.soumu.go.jp\/toukei_toukatsu\/index\/seido\/9-5.htm', 'a[href*=".pdf"]'); \/\/ OK\n await download('https:\/\/pdf-xml-download-test.vercel.app\/', '#link-pdf'); \/\/ OK\n await download('https:\/\/pdf-xml-download-test.vercel.app\/', '#link-xml'); \/\/ OK\n})();<\/code><\/pre>\n
\u65b9\u6cd54\u3010\u00d7\u3011: response.buffer\u3092\u5229\u7528<\/h3>\n
\n\u3053\u306e\u65b9\u6cd5\u306f\u3001response.body\u3092stream download\u3067\u304d\u308b\u3060\u3051\u3067\u3001\u30d5\u30a1\u30a4\u30eb\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306e\u30b1\u30fc\u30b9\u3084PDF\u30d5\u30a1\u30a4\u30eb\u306e\u3088\u3046\u306b<embeded><\/code>\u3067\u8aad\u307f\u8fbc\u3080\u30b1\u30fc\u30b9\u3067\u306f\u3046\u307e\u304f\u52d5\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n
const puppeteer = require('puppeteer');\nconst fs = require('fs');\n\n(async () => {\n async function downlaod(targetUrl) {\n const browser = await puppeteer.launch();\n const page = await browser.newPage(); \/\/ \u30da\u30fc\u30b8\u3092\u958b\u304f\n const outputPath = `downloads\/${targetUrl.match(\/([^\/]+\\.[^\/]+)($|\\?)\/)[1]}`; \/\/ URL\u304b\u3089\u30d5\u30a1\u30a4\u30eb\u540d\u53d6\u5f97\u3057\u3066\u3001\u51fa\u529b\u30d1\u30b9\n\n await page.setRequestInterception(true); \/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30a4\u30f3\u30bf\u30fc\u30bb\u30d7\u30c8\n let isFirstRequest = true;\n page.on('request', request => {\n if (!isFirstRequest && request.isNavigationRequest()) { \/\/ \u521d\u56de\u30ea\u30af\u30a8\u30b9\u30c8\u4ee5\u5916\u3067\u753b\u9762\u9077\u79fb\u306e\u5834\u5408\u306f\u7d42\u4e86\n isFirstRequest = false;\n return request.abort();\n }\n request.continue(); \/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u7d99\u7d9a\u3059\u308b\n });\n page.on('response', async (response) => {\n if (response.url() === targetUrl) {\n const buffer = await response.buffer();\n fs.writeFile(outputPath, buffer, (err) => {\n if (err) throw err;\n });\n }\n });\n\n await page.goto(targetUrl);\n await browser.close();\n }\n\n await downlaod('https:\/\/www.soumu.go.jp\/menu_syokai\/index.html'); \/\/ OK\n await downlaod('https:\/\/www.soumu.go.jp\/main_content\/000269738.jpg'); \/\/ OK\n await downlaod('https:\/\/www.soumu.go.jp\/main_content\/000608358.csv'); \/\/ NG: \u300cError: Protocol error (Network.getResponseBody): No resource with given identifier found\u300d\u304c\u767a\u751f\u3059\u308b\n await downlaod('https:\/\/www.soumu.go.jp\/main_content\/000323620.pdf'); \/\/ NG: \u300cError: Protocol error (Network.getResponseBody): No resource with given identifier found\u300d\u304c\u767a\u751f\u3059\u308b\n})();<\/code><\/pre>\n
browser.on('targetcreated', function)<\/code>\u3092\u5229\u7528\u3059\u308b\u65b9\u6cd5\u3082\u3042\u308a\u305d\u3046\u3067\u3059\u304c\u3001\u65b9\u6cd53\u3088\u308a\u5b8c\u74a7\u306b\u52d5\u304f\u611f\u3058\u3067\u306f\u306a\u3044\u3068\u601d\u308f\u308c\u308b\u306e\u3067\u8a66\u3057\u3066\u304a\u308a\u307e\u305b\u3093\u3002<\/p>\n
\u305d\u306e\u4ed6<\/h2>\n
\u30da\u30fc\u30b8\u306e\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8\u3092\u53d6\u308b<\/h3>\n
const puppeteer = require('puppeteer');\n\n(async () => {\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n await page.goto('https:\/\/www.google.com\/');\n await page.screenshot({ path: 'downloads\/sample1.png' })\n await browser.close();\n})();<\/code><\/pre>\n
\u30da\u30fc\u30b8\u3092PDF\u3068\u3057\u3066\u4fdd\u5b58\u3059\u308b<\/h3>\n
\nheadless=false\u306b\u3059\u308b\u3068\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u305d\u3053\u3060\u3051\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002<\/p>\nconst puppeteer = require('puppeteer');\n\n(async () => {\n const browser = await puppeteer.launch(); \/\/ NOTE: headless=true\u3058\u3083\u306a\u3044\u3068\u3001\u300cUnhandledPromiseRejectionWarning: Error: Protocol error (Page.printToPDF): PrintToPDF is not implemented\u300d\u304c\u767a\u751f\u3059\u308b\n const page = await browser.newPage();\n await page.goto('https:\/\/www.google.com\/');\n await page.pdf({ path: 'downloads\/sample2.pdf', format: 'A4' })\n await browser.close();\n})();<\/code><\/pre>\n
\u3055\u3044\u3054\u306b<\/h2>\n
\n\u4eca\u56de\u8abf\u3079\u3066\u307f\u3066\u3001\u5b9f\u52d9\u3067\u3082\u306a\u3093\u3068\u304b\u4f7f\u3048\u305d\u3046\u306a\u3084\u308a\u65b9\uff08\u65b9\u6cd53\uff09\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u3066\u3088\u304b\u3063\u305f\u3067\u3059\u3002<\/p>\n
\nhttps:\/\/github.com\/rinoguchi\/python_scraping_sample\/tree\/master\/src\/puppeteer<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"