01-app.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * 带下载小说
  3. * https://www.qu.la/book/16431/ 一念永恒
  4. * https://www.qu.la/book/513/ 仙路争锋
  5. * https://www.qu.la/book/365/ 武动乾坤
  6. */
  7. const puppeteer = require('puppeteer');
  8. const fs = require("fs");
  9. (async () => {
  10. const browser = await puppeteer.launch({
  11. headless: true, //默认为true(无头),不显示浏览器界面
  12. // slowMo: 200, //减速显示,有时会作为模拟人操作特意减速
  13. // devtools: true //显示开发者工具。页面宽高默认800*600,把开发者工具显示再隐藏页面会占满屏幕,有没有大佬解释下?
  14. });
  15. /**
  16. * document.querySelector('.liebiao').children[0].children[0].children[0].getAttribute('href')
  17. * document.querySelector('.liebiao').children[0].children[0].innerText
  18. */
  19. //生成Page对象
  20. //const page = await browser.newPage();//官网写法:一打开浏览器会打开两个tab,第二个才是你正在操作的tab
  21. const page = (await browser.pages())[0]; //这是我的写法,只有一个tab
  22. await page.goto('https://www.qu.la/book/4292/'); //跳转到掘金
  23. //请开始你的表演...
  24. const result = await page.evaluate(() => {
  25. return new Promise(resolve => {
  26. let $titles = document.getElementById('list').getElementsByTagName('dd');
  27. let titles = [];
  28. let index = 0;
  29. const len = $titles.length;
  30. const interval = setInterval(() => {
  31. if (index === len) {
  32. resolve(titles);
  33. clearInterval(interval);
  34. return;
  35. }
  36. titles.push({
  37. href: `https://www.qu.la${$titles[index].children[0].getAttribute('href')}`,
  38. title: $titles[index].children[0].innerText,
  39. index: index + 1
  40. });
  41. index += 1;
  42. }, 10);
  43. });
  44. });
  45. await browser.close(); //关闭浏览器
  46. console.log('表演结束!');
  47. fs.writeFileSync('./xindaming.js', 'module.exports =' + JSON.stringify(result));
  48. })();