Last active
May 20, 2024 14:37
-
-
Save VityaSchel/28f1a360ee7798511765910b39c6086c to your computer and use it in GitHub Desktop.
Парсер сайта колледжа связи https://ks.psuti.ru/ и https://lk.ks.psuti.ru/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(window.location.href !== 'https://ks.psuti.ru/about/teachers.html') throw new Error('Перейдите на страницу https://ks.psuti.ru/about/teachers.html') | |
const table = document.querySelector('form[action="https://ks.psuti.ru/about/teachers.html"') | |
const limitField = table.querySelector('select[name="limit"]') | |
if(limitField.options[limitField.selectedIndex].text !== 'Все') throw new Error('Выберете "Все" в "Количество строк"') | |
const teachers = Array.from(table.querySelectorAll('tbody > tr > td:last-child > a')).slice(1) | |
const getTeacherInfo = (url) => { | |
return new Promise((resolve, reject) => { | |
const handle = window.open(url) | |
const onLoaded = async () => { | |
try { | |
resolve(handle.document.querySelector('.article-content img').src) | |
handle.close() | |
} catch(e) { | |
reject(e) | |
} | |
} | |
let intervalID = setInterval(() => { | |
try { | |
if (handle && handle.document && handle.location.href !== 'about:blank') { | |
clearInterval(intervalID) | |
if (['complete', 'interactive'].includes(handle.document.readyState)) { | |
onLoaded() | |
} else { | |
handle.addEventListener('DOMContentLoaded', onLoaded) | |
} | |
} | |
} catch (e) { | |
// Keep waiting for load | |
} | |
}, 100) | |
}) | |
} | |
// beware as it opens a lot of windows simultaniously | |
console.log(await Promise.all(teachers.map(async t => ({ name: t.textContent.trim(), picture: await getTeacherInfo(t.getAttribute('href')) })))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created for https://github.com/VityaSchel/kspguti-schedule