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.










share|improve this question



























    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.










    share|improve this question

























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 18:34









      Grant Miller

      4,749132447




      4,749132447










      asked Nov 10 at 16:51









      Inkweon Kim

      133




      133






















          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.');


          // ...

          );





          share|improve this answer




















          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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

























          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.');


          // ...

          );





          share|improve this answer




















          • 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














          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.');


          // ...

          );





          share|improve this answer




















          • 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












          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.');


          // ...

          );





          share|improve this answer












          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.');


          // ...

          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto