How to print an HTML document using Puppeteer?
up vote
1
down vote
favorite
Recently I started to crawl the web using Puppeteer. Below is a code for extracting a specific product name from the shopping mall.
const puppeteer = require('puppeteer');
(async () =>
const width = 1600, height = 1040;
const option = headless: false, slowMo: true, args: [`--window-size=$width,$height`] ;
const browser = await puppeteer.launch(option);
const page = await browser.newPage();
const vp = width: width, height: height;
await page.setViewport(vp);
const navigationPromise = page.waitForNavigation();
await page.goto('https://shopping.naver.com/home/p/index.nhn');
await navigationPromise;
await page.waitFor(2000);
const textBoxId = 'co_srh_input';
await page.type('.' + textBoxId, '양말', delay: 100);
await page.keyboard.press('Enter');
await page.waitFor(5000);
await page.waitForSelector('div.info > a.tit');
const stores = await page.evaluate(() =>
const links = Array.from(document.querySelectorAll('div.info > a.tit'));
return links.map(link => link.innerText).slice(0, 10) // 10개 제품만 가져오기
);
console.log(stores);
await browser.close();
)();
I have a question. How can I output the crawled results to an HTML document (without using the database)? Please use sample code to explain it.
javascript node.js web-crawler google-chrome-devtools puppeteer
add a comment |
up vote
1
down vote
favorite
Recently I started to crawl the web using Puppeteer. Below is a code for extracting a specific product name from the shopping mall.
const puppeteer = require('puppeteer');
(async () =>
const width = 1600, height = 1040;
const option = headless: false, slowMo: true, args: [`--window-size=$width,$height`] ;
const browser = await puppeteer.launch(option);
const page = await browser.newPage();
const vp = width: width, height: height;
await page.setViewport(vp);
const navigationPromise = page.waitForNavigation();
await page.goto('https://shopping.naver.com/home/p/index.nhn');
await navigationPromise;
await page.waitFor(2000);
const textBoxId = 'co_srh_input';
await page.type('.' + textBoxId, '양말', delay: 100);
await page.keyboard.press('Enter');
await page.waitFor(5000);
await page.waitForSelector('div.info > a.tit');
const stores = await page.evaluate(() =>
const links = Array.from(document.querySelectorAll('div.info > a.tit'));
return links.map(link => link.innerText).slice(0, 10) // 10개 제품만 가져오기
);
console.log(stores);
await browser.close();
)();
I have a question. How can I output the crawled results to an HTML document (without using the database)? Please use sample code to explain it.
javascript node.js web-crawler google-chrome-devtools puppeteer
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Recently I started to crawl the web using Puppeteer. Below is a code for extracting a specific product name from the shopping mall.
const puppeteer = require('puppeteer');
(async () =>
const width = 1600, height = 1040;
const option = headless: false, slowMo: true, args: [`--window-size=$width,$height`] ;
const browser = await puppeteer.launch(option);
const page = await browser.newPage();
const vp = width: width, height: height;
await page.setViewport(vp);
const navigationPromise = page.waitForNavigation();
await page.goto('https://shopping.naver.com/home/p/index.nhn');
await navigationPromise;
await page.waitFor(2000);
const textBoxId = 'co_srh_input';
await page.type('.' + textBoxId, '양말', delay: 100);
await page.keyboard.press('Enter');
await page.waitFor(5000);
await page.waitForSelector('div.info > a.tit');
const stores = await page.evaluate(() =>
const links = Array.from(document.querySelectorAll('div.info > a.tit'));
return links.map(link => link.innerText).slice(0, 10) // 10개 제품만 가져오기
);
console.log(stores);
await browser.close();
)();
I have a question. How can I output the crawled results to an HTML document (without using the database)? Please use sample code to explain it.
javascript node.js web-crawler google-chrome-devtools puppeteer
Recently I started to crawl the web using Puppeteer. Below is a code for extracting a specific product name from the shopping mall.
const puppeteer = require('puppeteer');
(async () =>
const width = 1600, height = 1040;
const option = headless: false, slowMo: true, args: [`--window-size=$width,$height`] ;
const browser = await puppeteer.launch(option);
const page = await browser.newPage();
const vp = width: width, height: height;
await page.setViewport(vp);
const navigationPromise = page.waitForNavigation();
await page.goto('https://shopping.naver.com/home/p/index.nhn');
await navigationPromise;
await page.waitFor(2000);
const textBoxId = 'co_srh_input';
await page.type('.' + textBoxId, '양말', delay: 100);
await page.keyboard.press('Enter');
await page.waitFor(5000);
await page.waitForSelector('div.info > a.tit');
const stores = await page.evaluate(() =>
const links = Array.from(document.querySelectorAll('div.info > a.tit'));
return links.map(link => link.innerText).slice(0, 10) // 10개 제품만 가져오기
);
console.log(stores);
await browser.close();
)();
I have a question. How can I output the crawled results to an HTML document (without using the database)? Please use sample code to explain it.
javascript node.js web-crawler google-chrome-devtools puppeteer
javascript node.js web-crawler google-chrome-devtools puppeteer
edited Nov 10 at 18:34
Grant Miller
4,749132447
4,749132447
asked Nov 10 at 16:51
Inkweon Kim
133
133
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
fs.writeFile()
You can use the following write_file
function that returns a Promise
that resolves or rejects when fs.writeFile()
succeeds or fails.
Then, you can await
the Promise
from within your anonymous, asynchronous function and check whether or not the data was written to the file:
'use strict';
const fs = require('fs');
const puppeteer = require('puppeteer');
const write_file = (file, data) => new Promise((resolve, reject) =>
fs.writeFile(file, data, 'utf8', error =>
if (error)
console.error(error);
reject(false);
else
resolve(true);
);
);
(async () =>
// ...
const stores = await page.evaluate(() =>
return Array.from(document.querySelectorAll('div.info > a.tit'), link => link.innerText).slice(0, 10); // 10개 제품만 가져오기
);
if (await write_file('example.html', stores.toString()) === false)
console.error('Error: Unable to write stores to example.html.');
// ...
);
thanks!! I solved the problem. If I want to insert into <li> inside an HTML document, Can I use Express?
– Inkweon Kim
Nov 10 at 18:52
Hi. would it be possible for you to please have a look at this ? stackoverflow.com/questions/53287662/…
– Eris
Nov 13 at 19:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
fs.writeFile()
You can use the following write_file
function that returns a Promise
that resolves or rejects when fs.writeFile()
succeeds or fails.
Then, you can await
the Promise
from within your anonymous, asynchronous function and check whether or not the data was written to the file:
'use strict';
const fs = require('fs');
const puppeteer = require('puppeteer');
const write_file = (file, data) => new Promise((resolve, reject) =>
fs.writeFile(file, data, 'utf8', error =>
if (error)
console.error(error);
reject(false);
else
resolve(true);
);
);
(async () =>
// ...
const stores = await page.evaluate(() =>
return Array.from(document.querySelectorAll('div.info > a.tit'), link => link.innerText).slice(0, 10); // 10개 제품만 가져오기
);
if (await write_file('example.html', stores.toString()) === false)
console.error('Error: Unable to write stores to example.html.');
// ...
);
thanks!! I solved the problem. If I want to insert into <li> inside an HTML document, Can I use Express?
– Inkweon Kim
Nov 10 at 18:52
Hi. would it be possible for you to please have a look at this ? stackoverflow.com/questions/53287662/…
– Eris
Nov 13 at 19:20
add a comment |
up vote
2
down vote
accepted
fs.writeFile()
You can use the following write_file
function that returns a Promise
that resolves or rejects when fs.writeFile()
succeeds or fails.
Then, you can await
the Promise
from within your anonymous, asynchronous function and check whether or not the data was written to the file:
'use strict';
const fs = require('fs');
const puppeteer = require('puppeteer');
const write_file = (file, data) => new Promise((resolve, reject) =>
fs.writeFile(file, data, 'utf8', error =>
if (error)
console.error(error);
reject(false);
else
resolve(true);
);
);
(async () =>
// ...
const stores = await page.evaluate(() =>
return Array.from(document.querySelectorAll('div.info > a.tit'), link => link.innerText).slice(0, 10); // 10개 제품만 가져오기
);
if (await write_file('example.html', stores.toString()) === false)
console.error('Error: Unable to write stores to example.html.');
// ...
);
thanks!! I solved the problem. If I want to insert into <li> inside an HTML document, Can I use Express?
– Inkweon Kim
Nov 10 at 18:52
Hi. would it be possible for you to please have a look at this ? stackoverflow.com/questions/53287662/…
– Eris
Nov 13 at 19:20
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
fs.writeFile()
You can use the following write_file
function that returns a Promise
that resolves or rejects when fs.writeFile()
succeeds or fails.
Then, you can await
the Promise
from within your anonymous, asynchronous function and check whether or not the data was written to the file:
'use strict';
const fs = require('fs');
const puppeteer = require('puppeteer');
const write_file = (file, data) => new Promise((resolve, reject) =>
fs.writeFile(file, data, 'utf8', error =>
if (error)
console.error(error);
reject(false);
else
resolve(true);
);
);
(async () =>
// ...
const stores = await page.evaluate(() =>
return Array.from(document.querySelectorAll('div.info > a.tit'), link => link.innerText).slice(0, 10); // 10개 제품만 가져오기
);
if (await write_file('example.html', stores.toString()) === false)
console.error('Error: Unable to write stores to example.html.');
// ...
);
fs.writeFile()
You can use the following write_file
function that returns a Promise
that resolves or rejects when fs.writeFile()
succeeds or fails.
Then, you can await
the Promise
from within your anonymous, asynchronous function and check whether or not the data was written to the file:
'use strict';
const fs = require('fs');
const puppeteer = require('puppeteer');
const write_file = (file, data) => new Promise((resolve, reject) =>
fs.writeFile(file, data, 'utf8', error =>
if (error)
console.error(error);
reject(false);
else
resolve(true);
);
);
(async () =>
// ...
const stores = await page.evaluate(() =>
return Array.from(document.querySelectorAll('div.info > a.tit'), link => link.innerText).slice(0, 10); // 10개 제품만 가져오기
);
if (await write_file('example.html', stores.toString()) === false)
console.error('Error: Unable to write stores to example.html.');
// ...
);
answered Nov 10 at 18:33
Grant Miller
4,749132447
4,749132447
thanks!! I solved the problem. If I want to insert into <li> inside an HTML document, Can I use Express?
– Inkweon Kim
Nov 10 at 18:52
Hi. would it be possible for you to please have a look at this ? stackoverflow.com/questions/53287662/…
– Eris
Nov 13 at 19:20
add a comment |
thanks!! I solved the problem. If I want to insert into <li> inside an HTML document, Can I use Express?
– Inkweon Kim
Nov 10 at 18:52
Hi. would it be possible for you to please have a look at this ? stackoverflow.com/questions/53287662/…
– Eris
Nov 13 at 19:20
thanks!! I solved the problem. If I want to insert into <li> inside an HTML document, Can I use Express?
– Inkweon Kim
Nov 10 at 18:52
thanks!! I solved the problem. If I want to insert into <li> inside an HTML document, Can I use Express?
– Inkweon Kim
Nov 10 at 18:52
Hi. would it be possible for you to please have a look at this ? stackoverflow.com/questions/53287662/…
– Eris
Nov 13 at 19:20
Hi. would it be possible for you to please have a look at this ? stackoverflow.com/questions/53287662/…
– Eris
Nov 13 at 19:20
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241203%2fhow-to-print-an-html-document-using-puppeteer%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown