Guest User

Untitled

a guest
Nov 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. const express = require('express');
  2. const puppeteer = require('puppeteer');
  3. const fs = require('fs');
  4.  
  5. const app = express();
  6.  
  7. (async () => {
  8.  
  9. const width = 1600, height = 1040;
  10. const option = { headless: true, slowMo: true, args: [`--window-size=${width},${height}`] };
  11. const browser = await puppeteer.launch(option);
  12. const page = await browser.newPage();
  13.  
  14. await page.goto('https://search.shopping.naver.com/search/all.nhn?query=%EC%96%91%EB%A7%90&cat_id=&frm=NVSHATC');
  15.  
  16. await page.waitFor(5000);
  17. await page.waitForSelector('ul.goods_list');
  18. await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'});
  19.  
  20. const naver = await page.evaluate(() => {
  21. const data = {
  22. "naver" : []
  23. };
  24.  
  25. $('ul.goods_list > li._itemSection').each(function () {
  26.  
  27. const title = $.trim($(this).find('div.info > a.tit').text());
  28. const price = $(this).find('div.info > .price .num').text();
  29. const image = $(this).find('div.img_area img').attr('src');
  30.  
  31. data.naver.push({ title, price, image })
  32.  
  33. });
  34.  
  35. return data;
  36. });
  37.  
  38. if (await write_file('example.json', JSON.stringify(naver)) === false) {
  39. console.error('Error: Unable to write stores to example.json');
  40. }
  41.  
  42. await browser.close();
  43.  
  44. })();
  45.  
  46. const write_file = (file, data) => new Promise((resolve, reject) => {
  47. fs.writeFile(file, data, 'utf8', error => {
  48. if (error) {
  49. console.error(error);
  50. reject(false);
  51. } else {
  52. resolve(true);
  53. }
  54. });
  55. });
  56.  
  57. app.listen(3000, () => console.log("Express!!!"));
Add Comment
Please, Sign In to add comment