sku-data.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. function skuData(driver) {
  6. return new Promise(async (resolve, reject) => {
  7. let typeList = await driver.findElements(webdriver.By.js(() => document.querySelectorAll('.tb-sku .tb-prop .J_TSaleProp')))
  8. let _sizeList = await typeList[0].findElements(webdriver.By.tagName('li:not(.tb-out-of-stock)'));
  9. const sizeList = await _sizeList.filter(async item => {
  10. const styleStr = await item.getAttribute('style');
  11. return styleStr.indexOf('none') !== -1;
  12. });
  13. let _colorList = await typeList[1].findElements(webdriver.By.tagName('li:not(.tb-out-of-stock)'));
  14. const colorList = await _colorList.filter(async function (item) {
  15. const styleStr = await item.getAttribute('style');
  16. return styleStr.indexOf('none') != -1;
  17. });
  18. let sku = [];
  19. let sizeIndex = 0;
  20. async function eachSize(sizeIndex) {
  21. const size = sizeList[sizeIndex];
  22. let sizeClass = await size.getAttribute('class');
  23. if (sizeClass.indexOf('tb-selected') === -1) {
  24. size.click();
  25. }
  26. let colorIndex = 0;
  27. function eachColor(colorIndex) {
  28. return new Promise((resolve2, reject2) => {
  29. async function eachColor2(colorIndex) {
  30. const color = colorList[colorIndex];
  31. let colorClass = await color.getAttribute('class');
  32. if (colorClass.indexOf('tb-selected') === -1) {
  33. color.click();
  34. }
  35. await driver.sleep(500);
  36. console.log(sizeIndex, colorIndex);
  37. const htmlStr = await driver.getPageSource();
  38. const d = getSkuData(htmlStr);
  39. if (d) {
  40. sku.push(d);
  41. }
  42. colorIndex++;
  43. console.log('colorIndex', colorIndex, colorList.length);
  44. if (colorIndex === colorList.length) {
  45. color.click();
  46. resolve2();
  47. } else {
  48. eachColor2(colorIndex);
  49. }
  50. }
  51. eachColor2(colorIndex)
  52. });
  53. }
  54. await driver.sleep(500);
  55. await eachColor(colorIndex);
  56. sizeIndex++;
  57. console.log('color完毕', sizeIndex, sizeList.length);
  58. if (sizeIndex === sizeList.length) {
  59. resolve(sku);
  60. } else {
  61. eachSize(sizeIndex);
  62. }
  63. }
  64. eachSize(sizeIndex)
  65. });
  66. }
  67. function getSkuData(htmlStr) {
  68. const data = {};
  69. const $ = cheerio.load(htmlStr);
  70. data.size = $('[data-property="尺码"] .tb-selected span').text() || $('[data-property="尺寸"] .tb-selected span').text();
  71. if (data.size.indexOf('加入购物车') != -1) {
  72. data.size = '默认'
  73. }
  74. data.color = $('[data-property="颜色分类"] .tb-selected span').text();
  75. console.log(['写入规格:', 'size:', data.size, 'color:', data.color]);
  76. data.bigImg = $('#J_ImgBooth').attr('src');
  77. data.colorImg = $('[data-property="颜色分类"] .tb-selected a').attr('style')
  78. // console.log([data.colorImg, 'colorImg']);
  79. data.colorImg = data.colorImg ? data.colorImg.replace(/(.+)\(([^\>)]+)\)(.+)/igm, '$2') : '';
  80. data.stock = $('#J_EmStock').text();
  81. // console.log([data.stock, 'stock']);
  82. data.stock = data.stock ? data.stock.replace(/[^\d]+(\d+)[^\d]+/, '$1') : '';
  83. return data;
  84. }
  85. exports.skuData = skuData;