sku-data.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const cheerio = require('cheerio');
  2. const webdriver = require('selenium-webdriver');
  3. var entities = require('html-entities').XmlEntities;
  4. const { saveFile } = require('./save-file');
  5. /**
  6. * sku获取
  7. * @param { WebDriver } driver
  8. */
  9. function skuData(driver) {
  10. let sku = []; // 输出用
  11. let codeSku = []; // 代码用
  12. return new Promise(async (resolve, reject) => {
  13. // 属性列表
  14. let typeList = await driver.findElements(webdriver.By.js(() => document.querySelectorAll(".tb-sku dl.tm-sale-prop")))
  15. for (let item of typeList) {
  16. const titleEle = await item.findElement(webdriver.By.className('tb-metatit'))
  17. const title = await titleEle.getText();
  18. const list = await item.findElements(webdriver.By.tagName("li:not(.tb-out-of-stock)"));
  19. let obj = {
  20. title,
  21. list: [],
  22. }
  23. let objCode = {
  24. title,
  25. list: [],
  26. index:0,
  27. num:list.length
  28. }
  29. for (let item2 of list) {
  30. const nameEle = await item2.findElement(webdriver.By.tagName('span'))
  31. const name = await nameEle.getText();
  32. const imgEle = await item2.findElement(webdriver.By.tagName('a'))
  33. let img = await imgEle.getAttribute('style');
  34. img = img ? img.replace(/(.+)\(([^\>)]+)\)(.+)/igm, '$2') : '';
  35. const itemObj = {
  36. title: name,
  37. img,
  38. }
  39. const itemObjCode = {
  40. title: name,
  41. img,
  42. ele:item
  43. }
  44. obj.list.push(itemObj)
  45. objCode.list.push(itemObjCode)
  46. }
  47. sku.push(obj);
  48. codeSku.push(objCode);
  49. }
  50. resolve(sku);
  51. // // 第一个为size
  52. // let _sizeList = await typeList[0].findElements(webdriver.By.tagName('li:not(.tb-out-of-stock)'));
  53. // const sizeList = await _sizeList.filter(async item => {
  54. // const styleStr = await item.getAttribute('style');
  55. // return styleStr.indexOf('none') !== -1;
  56. // });
  57. // // 第二个为color
  58. // let _colorList = await typeList[1].findElements(webdriver.By.tagName('li:not(.tb-out-of-stock)'));
  59. // const colorList = await _colorList.filter(async function (item) {
  60. // const styleStr = await item.getAttribute('style');
  61. // return styleStr.indexOf('none') != -1;
  62. // });
  63. // let sku = [];
  64. // let sizeIndex = 0;
  65. // // 遍历 size
  66. // async function eachSize(sizeIndex) {
  67. // const size = sizeList[sizeIndex];
  68. // // 如果没有禁用类名 则模拟点击
  69. // let sizeClass = await size.getAttribute('class');
  70. // if (sizeClass.indexOf('tb-selected') === -1) {
  71. // size.click();
  72. // }
  73. // let colorIndex = 0;
  74. // // 遍历 color
  75. // function eachColor(colorIndex) {
  76. // return new Promise((resolve2, reject2) => {
  77. // async function eachColor2(colorIndex) {
  78. // const color = colorList[colorIndex];
  79. // // 如果没有禁用类名 则模拟点击
  80. // let colorClass = await color.getAttribute('class');
  81. // if (colorClass.indexOf('tb-selected') === -1) {
  82. // color.click();
  83. // }
  84. // await driver.sleep(500);
  85. // console.log(sizeIndex, colorIndex);
  86. // const htmlStr = await driver.getPageSource();
  87. // console.log('sku', colorIndex, sizeIndex,'总数',colorList.length,sizeList.length);
  88. // const d = getSkuData(htmlStr);
  89. // if (d) {
  90. // sku.push(d);
  91. // }
  92. // colorIndex++;
  93. // if (colorIndex === colorList.length) {
  94. // color.click();
  95. // resolve2();
  96. // } else {
  97. // eachColor2(colorIndex);
  98. // }
  99. // }
  100. // eachColor2(colorIndex)
  101. // });
  102. // }
  103. // await driver.sleep(500);
  104. // await eachColor(colorIndex);
  105. // sizeIndex++;
  106. // // console.log('color完毕', sizeIndex, sizeList.length);
  107. // if (sizeIndex === sizeList.length) {
  108. // resolve(sku);
  109. // } else {
  110. // eachSize(sizeIndex);
  111. // }
  112. // }
  113. // eachSize(sizeIndex)
  114. });
  115. }
  116. /**
  117. * 获取sku 值
  118. * @param { string } htmlStr
  119. */
  120. function getSkuData(htmlStr) {
  121. const data = {};
  122. const $ = cheerio.load(htmlStr);
  123. data.size = $('[data-property="尺码"] .tb-selected span').text() || $('[data-property="尺寸"] .tb-selected span').text();
  124. if (data.size.indexOf('加入购物车') != -1) {
  125. data.size = '默认'
  126. }
  127. data.color = $('[data-property="颜色分类"] .tb-selected span').text();
  128. console.log(['写入规格:', 'size:', data.size, 'color:', data.color]);
  129. data.bigImg = $('#J_ImgBooth').attr('src');
  130. data.colorImg = $('[data-property="颜色分类"] .tb-selected a').attr('style')
  131. // console.log([data.colorImg, 'colorImg']);
  132. data.colorImg = data.colorImg ? data.colorImg.replace(/(.+)\(([^\>)]+)\)(.+)/igm, '$2') : '';
  133. data.stock = $('#J_EmStock').text();
  134. // console.log([data.stock, 'stock']);
  135. data.stock = data.stock ? data.stock.replace(/[^\d]+(\d+)[^\d]+/, '$1') : '';
  136. return data;
  137. }
  138. exports.skuData = skuData;