04-page.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const fs = require("fs");
  2. const puppeteer = require('puppeteer');
  3. const shell = require('shelljs');
  4. const utils = require('./utils/base');
  5. var $xindaming = require('./xindaming');
  6. const len = $xindaming.length;
  7. let index = 1019;
  8. const createPage = async (url) => {
  9. const browser = await puppeteer.launch({
  10. args: ['--no-sandbox', '--disable-setuid-sandbox'], // 沙箱模式下运行
  11. headless: true, //默认为true(无头),不显示浏览器界面
  12. // slowMo: 200,
  13. });
  14. const page = (await browser.pages())[0]; //这是我的写法,只有一个tab
  15. await page.goto(url); //跳转到掘金
  16. const result = await page.evaluate(() => {
  17. return new Promise(resolve => {
  18. let content = {
  19. title: document.getElementsByTagName('h1')[0].innerText,
  20. page: document.getElementById('content').innerText
  21. };
  22. resolve(content);
  23. });
  24. });
  25. await browser.close(); //关闭浏览器
  26. return result;
  27. };
  28. forEachUrl();
  29. function forEachUrl() {
  30. if ($xindaming[index] !== undefined) {
  31. console.clear();
  32. console.log(`还剩${len - index}; 当前进度:${$xindaming[index].index} ${$xindaming[index].title}`);
  33. createPage($xindaming[index].href).then(res => {
  34. return utils.page($xindaming[index], res);
  35. }).then(res => {
  36. fs.writeFileSync('./book/OEBPS/Text/text' + $xindaming[index].index + '.xhtml', res);
  37. setTimeout(() => {
  38. index += 1;
  39. forEachUrl();
  40. }, 30);
  41. });
  42. } else {
  43. shell.exec(`zip -p -r ${utils.name}.epub book`);
  44. // linux 下执行命令
  45. // shell.exec(`./utils/kindlegen -c1 ${utils.name}.epub -locale zh`);
  46. // mac下执行命令
  47. shell.exec(`/Users/honghaitao/Applications/KindleGen_Mac_i386_v2_9/kindlegen -c1 ${utils.name}.epub -locale zh`);
  48. // shell.exec(`rm ${utils.name}.epub`);
  49. }
  50. }