base-data.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const cheerio = require('cheerio');
  2. var entities = require('html-entities').XmlEntities;
  3. /**
  4. * 保存 页面基础数据
  5. * @param { WebDriver } driver
  6. */
  7. function baseData(driver) {
  8. return new Promise((resolve, reject) => {
  9. driver.getPageSource().then(async htmlStr => {
  10. const data = {};
  11. const $ = cheerio.load(htmlStr);
  12. data.title = $('.tb-detail-hd h1').text();
  13. data.title = data.title.replace(/(\t|\n)/igm, '');
  14. data.price = $('#J_StrPriceModBox .tm-price').text();
  15. data.price2 = $('#J_PromoPrice .tm-price').text();
  16. data.address = $('#J_deliveryAdd').text();
  17. // 品牌
  18. data.brand = $('#J_AttrList .name b').text();
  19. // 参数
  20. data.info = [];
  21. $('#J_AttrUL li').each((i, e) => {
  22. const htmlArr = $(e).text().split(':');
  23. data.info.push({
  24. key: htmlArr[0],
  25. val: htmlArr[1]
  26. })
  27. })
  28. // 详情
  29. data.details = [];
  30. $('.content p img').each((i, e) => {
  31. data.details.push($(e).attr('src'));
  32. });
  33. // id
  34. const url = await driver.getCurrentUrl();
  35. data.id = url.replace(/^.+id=(\d+)&.+$/igm, '$1');
  36. resolve(data);
  37. });
  38. });
  39. }
  40. exports.baseData = baseData;