123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- const cheerio = require('cheerio');
- const webdriver = require('selenium-webdriver');
- var entities = require('html-entities').XmlEntities;
- const { saveFile } = require('./save-file');
- /**
- * sku获取
- * @param { WebDriver } driver
- */
- function skuData(driver) {
- let sku = []; // 输出用
- let codeSku = []; // 代码用
- return new Promise(async (resolve, reject) => {
- // 属性列表
- let typeList = await driver.findElements(webdriver.By.js(() => document.querySelectorAll(".tb-sku dl.tm-sale-prop")))
- for (let item of typeList) {
- const titleEle = await item.findElement(webdriver.By.className('tb-metatit'))
- const title = await titleEle.getText();
- const list = await item.findElements(webdriver.By.tagName("li:not(.tb-out-of-stock)"));
- let obj = {
- title,
- list: [],
- }
- let objCode = {
- title,
- list: [],
- index:0,
- num:list.length
- }
- for (let item2 of list) {
- const nameEle = await item2.findElement(webdriver.By.tagName('span'))
- const name = await nameEle.getText();
- const imgEle = await item2.findElement(webdriver.By.tagName('a'))
- let img = await imgEle.getAttribute('style');
- img = img ? img.replace(/(.+)\(([^\>)]+)\)(.+)/igm, '$2') : '';
- const itemObj = {
- title: name,
- img,
- }
- const itemObjCode = {
- title: name,
- img,
- ele:item
- }
- obj.list.push(itemObj)
- objCode.list.push(itemObjCode)
- }
- sku.push(obj);
- codeSku.push(objCode);
- }
-
- resolve(sku);
- // // 第一个为size
- // let _sizeList = await typeList[0].findElements(webdriver.By.tagName('li:not(.tb-out-of-stock)'));
- // const sizeList = await _sizeList.filter(async item => {
- // const styleStr = await item.getAttribute('style');
- // return styleStr.indexOf('none') !== -1;
- // });
- // // 第二个为color
- // let _colorList = await typeList[1].findElements(webdriver.By.tagName('li:not(.tb-out-of-stock)'));
- // const colorList = await _colorList.filter(async function (item) {
- // const styleStr = await item.getAttribute('style');
- // return styleStr.indexOf('none') != -1;
- // });
- // let sku = [];
- // let sizeIndex = 0;
- // // 遍历 size
- // async function eachSize(sizeIndex) {
- // const size = sizeList[sizeIndex];
- // // 如果没有禁用类名 则模拟点击
- // let sizeClass = await size.getAttribute('class');
- // if (sizeClass.indexOf('tb-selected') === -1) {
- // size.click();
- // }
- // let colorIndex = 0;
- // // 遍历 color
- // function eachColor(colorIndex) {
- // return new Promise((resolve2, reject2) => {
- // async function eachColor2(colorIndex) {
- // const color = colorList[colorIndex];
- // // 如果没有禁用类名 则模拟点击
- // let colorClass = await color.getAttribute('class');
- // if (colorClass.indexOf('tb-selected') === -1) {
- // color.click();
- // }
- // await driver.sleep(500);
- // console.log(sizeIndex, colorIndex);
- // const htmlStr = await driver.getPageSource();
- // console.log('sku', colorIndex, sizeIndex,'总数',colorList.length,sizeList.length);
- // const d = getSkuData(htmlStr);
- // if (d) {
- // sku.push(d);
- // }
- // colorIndex++;
- // if (colorIndex === colorList.length) {
- // color.click();
- // resolve2();
- // } else {
- // eachColor2(colorIndex);
- // }
- // }
- // eachColor2(colorIndex)
- // });
- // }
- // await driver.sleep(500);
- // await eachColor(colorIndex);
- // sizeIndex++;
- // // console.log('color完毕', sizeIndex, sizeList.length);
- // if (sizeIndex === sizeList.length) {
- // resolve(sku);
- // } else {
- // eachSize(sizeIndex);
- // }
- // }
- // eachSize(sizeIndex)
- });
- }
- /**
- * 获取sku 值
- * @param { string } htmlStr
- */
- function getSkuData(htmlStr) {
- const data = {};
- const $ = cheerio.load(htmlStr);
- data.size = $('[data-property="尺码"] .tb-selected span').text() || $('[data-property="尺寸"] .tb-selected span').text();
- if (data.size.indexOf('加入购物车') != -1) {
- data.size = '默认'
- }
- data.color = $('[data-property="颜色分类"] .tb-selected span').text();
- console.log(['写入规格:', 'size:', data.size, 'color:', data.color]);
- data.bigImg = $('#J_ImgBooth').attr('src');
- data.colorImg = $('[data-property="颜色分类"] .tb-selected a').attr('style')
- // console.log([data.colorImg, 'colorImg']);
- data.colorImg = data.colorImg ? data.colorImg.replace(/(.+)\(([^\>)]+)\)(.+)/igm, '$2') : '';
- data.stock = $('#J_EmStock').text();
- // console.log([data.stock, 'stock']);
- data.stock = data.stock ? data.stock.replace(/[^\d]+(\d+)[^\d]+/, '$1') : '';
- return data;
- }
- exports.skuData = skuData;
|