sku-data.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const cheerio = require('cheerio');
  2. const webdriver = require('selenium-webdriver');
  3. var entities = require('html-entities').XmlEntities;
  4. const { saveFile } = require('../tools/save-file');
  5. /**
  6. * sku获取
  7. * @param { WebDriver } driver
  8. */
  9. function skuData(driver) {
  10. return new Promise(async (resolve, reject) => {
  11. // 属性列表
  12. let typeList = await driver.findElements(webdriver.By.js(() => document.querySelectorAll('.tb-sku .tb-prop .J_TSaleProp')))
  13. // 第一个为size
  14. let _sizeList = await typeList[0].findElements(webdriver.By.tagName('li:not(.tb-out-of-stock)'));
  15. const sizeList = await _sizeList.filter(async item => {
  16. const styleStr = await item.getAttribute('style');
  17. return styleStr.indexOf('none') !== -1;
  18. });
  19. // 第二个为color
  20. let _colorList = await typeList[1].findElements(webdriver.By.tagName('li:not(.tb-out-of-stock)'));
  21. const colorList = await _colorList.filter(async function (item) {
  22. const styleStr = await item.getAttribute('style');
  23. return styleStr.indexOf('none') != -1;
  24. });
  25. let sku = [];
  26. let sizeIndex = 0;
  27. // 遍历 size
  28. async function eachSize(sizeIndex) {
  29. const size = sizeList[sizeIndex];
  30. // 如果没有禁用类名 则模拟点击
  31. let sizeClass = await size.getAttribute('class');
  32. if (sizeClass.indexOf('tb-selected') === -1) {
  33. size.click();
  34. }
  35. let colorIndex = 0;
  36. // 遍历 color
  37. function eachColor(colorIndex) {
  38. return new Promise((resolve2, reject2) => {
  39. async function eachColor2(colorIndex) {
  40. const color = colorList[colorIndex];
  41. // 如果没有禁用类名 则模拟点击
  42. let colorClass = await color.getAttribute('class');
  43. if (colorClass.indexOf('tb-selected') === -1) {
  44. color.click();
  45. }
  46. await driver.sleep(500);
  47. console.log(sizeIndex, colorIndex);
  48. const htmlStr = await driver.getPageSource();
  49. console.log('sku', colorIndex, sizeIndex,'总数',colorList.length,sizeList.length);
  50. const d = getSkuData(htmlStr);
  51. if (d) {
  52. sku.push(d);
  53. }
  54. colorIndex++;
  55. if (colorIndex === colorList.length) {
  56. color.click();
  57. resolve2();
  58. } else {
  59. eachColor2(colorIndex);
  60. }
  61. }
  62. eachColor2(colorIndex)
  63. });
  64. }
  65. await driver.sleep(500);
  66. await eachColor(colorIndex);
  67. sizeIndex++;
  68. // console.log('color完毕', sizeIndex, sizeList.length);
  69. if (sizeIndex === sizeList.length) {
  70. resolve(sku);
  71. } else {
  72. eachSize(sizeIndex);
  73. }
  74. }
  75. eachSize(sizeIndex)
  76. });
  77. }
  78. /**
  79. * 获取sku 值
  80. * @param { string } htmlStr
  81. */
  82. function getSkuData(htmlStr) {
  83. const data = {};
  84. const $ = cheerio.load(htmlStr);
  85. data.size = $('[data-property="尺码"] .tb-selected span').text() || $('[data-property="尺寸"] .tb-selected span').text();
  86. if (data.size.indexOf('加入购物车') != -1) {
  87. data.size = '默认'
  88. }
  89. data.color = $('[data-property="颜色分类"] .tb-selected span').text();
  90. console.log(['写入规格:', 'size:', data.size, 'color:', data.color]);
  91. data.bigImg = $('#J_ImgBooth').attr('src');
  92. data.colorImg = $('[data-property="颜色分类"] .tb-selected a').attr('style')
  93. // console.log([data.colorImg, 'colorImg']);
  94. data.colorImg = data.colorImg ? data.colorImg.replace(/(.+)\(([^\>)]+)\)(.+)/igm, '$2') : '';
  95. data.stock = $('#J_EmStock').text();
  96. // console.log([data.stock, 'stock']);
  97. data.stock = data.stock ? data.stock.replace(/[^\d]+(\d+)[^\d]+/, '$1') : '';
  98. return data;
  99. }
  100. exports.skuData = skuData;