app.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const webdriver = require('selenium-webdriver');
  2. const chrome = require('selenium-webdriver/chrome');
  3. const firefox = require('selenium-webdriver/firefox');
  4. const { saveFile } = require('./tools/save-file');
  5. const { baseData } = require('./tools/base-data');
  6. const { skuData } = require('./tools/sku-data');
  7. const { videoData } = require('./tools/video-data');
  8. const { imgsData } = require('./tools/imgs-data');
  9. const { downImg } = require('./tools/down-img');
  10. (async function example() {
  11. // let driver = await new Builder().forBrowser('chrome').build();
  12. let driver = new webdriver.Builder()
  13. .forBrowser('chrome')
  14. .build();
  15. driver.get('https://detail.tmall.com/item.htm?spm=a1z10.3-b-s.w4011-15438998729.159.167f3b511BHh5a&id=617256520600&rn=062ba2e2e6d139709e3e0e3505b56bbe&abbucket=3');
  16. // 检查登录
  17. await driver.wait(webdriver.until.elementLocated(webdriver.By.className('j_Username')), 100000, '超时未登录');
  18. console.log('登陆成功');
  19. driver.executeScript(function () {
  20. let y = 0;
  21. let timer = setInterval(() => {
  22. y += 50;
  23. if (y > 20000) {
  24. clearInterval(timer);
  25. }
  26. window.scrollTo(0, y)
  27. }, 10)
  28. })
  29. // 检查基本信息(判断详情加载完成)
  30. await driver.sleep(20000 / 50 * 10)
  31. console.log('滚动完成');
  32. // 判断是否有验证框
  33. try {
  34. const dialog = await driver.findElement(webdriver.By.className('baxia-dialog'));
  35. if (dialog) {
  36. const ele = await driver.findElement(webdriver.By.js(() => document.querySelector('.baxia-dialog .baxia-dialog-close')))
  37. ele.click();
  38. }
  39. console.log('有验证框');
  40. } catch (error) {
  41. }
  42. // // 延时一秒
  43. await driver.sleep(1000)
  44. let data = {};
  45. // 获取基础详情
  46. const bData = await baseData(driver);
  47. data = { ...bData };
  48. // imgs
  49. data.imgs = await imgsData(driver);
  50. // sku
  51. data.sku = await skuData(driver);
  52. console.log('开始保存')
  53. // video TODO: 视频用的blob暂不处理
  54. // data.video = await videoData(driver);
  55. saveFile(`test.html`, JSON.stringify(data, null, 4));
  56. downImg(data)
  57. })();