12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- const cheerio = require('cheerio');
- var entities = require('html-entities').XmlEntities;
- /**
- * 保存 页面基础数据
- * @param { WebDriver } driver
- */
- function baseData(driver) {
- return new Promise((resolve, reject) => {
- driver.getPageSource().then(async htmlStr => {
- const data = {};
- const $ = cheerio.load(htmlStr);
- data.title = $('.tb-detail-hd h1').text();
- data.title = data.title.replace(/(\t|\n)/igm, '');
- data.price = $('#J_StrPriceModBox .tm-price').text();
- data.price2 = $('#J_PromoPrice .tm-price').text();
- data.address = $('#J_deliveryAdd').text();
- // 品牌
- data.brand = $('#J_AttrList .name b').text();
- // 参数
- data.info = [];
- $('#J_AttrUL li').each((i, e) => {
- const htmlArr = $(e).text().split(':');
- data.info.push({
- key: htmlArr[0],
- val: htmlArr[1]
- })
- })
- // 详情
- data.details = [];
- $('.content p img').each((i, e) => {
- data.details.push($(e).attr('src'));
- });
- // id
- const url = await driver.getCurrentUrl();
- data.id = url.replace(/^.+id=(\d+)&.+$/igm, '$1');
- resolve(data);
- });
- });
- }
- exports.baseData = baseData;
|