01-app.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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: false, //默认为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/168/'); //跳转到掘金
  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. console.log($titles[index].children[0].innerText);
  37. titles.push({
  38. href: `https://www.qu.la${$titles[index].children[0].getAttribute('href')}`,
  39. title: $titles[index].children[0].innerText,
  40. index: index + 1
  41. });
  42. index += 1;
  43. }, 100);
  44. });
  45. });
  46. // await browser.close(); //关闭浏览器
  47. console.log('over');
  48. fs.writeFileSync('./xindaming.js', 'module.exports =' + JSON.stringify(result));
  49. })();