Advertisement
Juffo-Wup

Untitled

Apr 7th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. const puppeteer = require('puppeteer');
  2.  
  3. async function scrapeUrl(url) {
  4. // Launch a new browser instance.
  5. //const browser = await puppeteer.launch({ headless: false });
  6. const browser = await puppeteer.launch();
  7.  
  8. // Create a new page in the browser.
  9. const page = await browser.newPage();
  10.  
  11. // Navigate to the URL.
  12. await page.goto(url, {waitUntil: 'networkidle2'});
  13.  
  14. // Enter the password
  15. await page.type('input[name="password"]', 'hardcodedpasswordrule,notunliketunnelsnakes');
  16.  
  17. // Wait for the sign in button to be ready
  18. await page.waitForSelector('#btn_login')
  19.  
  20. // Click the sign in button
  21. await page.click('#btn_login');
  22.  
  23. // Wait for the next page to load
  24. await page.waitForNavigation({
  25. waitUntil: 'networkidle0',
  26. });
  27.  
  28. // Click the connected devices tab
  29. await page.click('#wrapper > main > div > div.inner_l > ul > li:nth-child(2) > a');
  30.  
  31. // Wait for the DOM to settle. Specifically, javascript populates
  32. // the desired values on the page every now and then, and none
  33. // of the other wait/timeout methods I tried actually made things
  34. // wait in the correct manner for actual data to get captured.
  35.  
  36. await waitForDOMToSettle(page);
  37.  
  38. // Extract the desired values
  39.  
  40. const pageTitle = await page.title();
  41. const currdev = await page.$eval(('.connectedUserCount'), span => span.textContent);
  42. const curremrg = await page.$eval(('.emergencyUserCount'), span => span.textContent);
  43. const peakdev_1hr = await page.$eval(('.peakConnectedUserCount'), span => span.textContent);
  44. const peakdev_24hr = await page.$eval(('.peakConnectedUserCount24'), span => span.textContent);
  45.  
  46. // Print the results
  47.  
  48. console.log(`Page Title: ${pageTitle}`);
  49. console.log(`Current devices connected: ${currdev}`);
  50. console.log(`Current emergency calls: ${curremrg}`);
  51. console.log(`Peak connections 1hr: ${peakdev_1hr}`);
  52. console.log(`Peak connections 24hr: ${peakdev_24hr}`);
  53.  
  54. // Close the browser instance.
  55. await browser.close();
  56. }
  57.  
  58. // Replace 'https://example.com' with the URL you want to scrape.
  59. scrapeUrl('http://pi2.rustytel.net:8020/#devices');
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. const waitForDOMToSettle = (page, timeoutMs = 30000, debounceMs = 1000) =>
  67. page.evaluate(
  68. (timeoutMs, debounceMs) => {
  69. let debounce = (func, ms = 1000) => {
  70. let timeout;
  71. return (...args) => {
  72. console.log("in debounce, clearing timeout again");
  73. clearTimeout(timeout);
  74. timeout = setTimeout(() => {
  75. func.apply(this, args);
  76. }, ms);
  77. };
  78. };
  79. return new Promise((resolve, reject) => {
  80. let mainTimeout = setTimeout(() => {
  81. observer.disconnect();
  82. reject(new Error("Timed out whilst waiting for DOM to settle"));
  83. }, timeoutMs);
  84.  
  85. let debouncedResolve = debounce(async () => {
  86. observer.disconnect();
  87. clearTimeout(mainTimeout);
  88. resolve();
  89. }, debounceMs);
  90.  
  91. const observer = new MutationObserver(() => {
  92. debouncedResolve();
  93. });
  94. const config = {
  95. attributes: true,
  96. childList: true,
  97. subtree: true,
  98. };
  99. observer.observe(document.body, config);
  100. });
  101. },
  102. timeoutMs,
  103. debounceMs
  104. );
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement