Skip to content

Instantly share code, notes, and snippets.

@VityaSchel
Last active May 20, 2024 14:37
Show Gist options
  • Save VityaSchel/28f1a360ee7798511765910b39c6086c to your computer and use it in GitHub Desktop.
Save VityaSchel/28f1a360ee7798511765910b39c6086c to your computer and use it in GitHub Desktop.
Парсер сайта колледжа связи https://ks.psuti.ru/ и https://lk.ks.psuti.ru/
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')) }))))
@VityaSchel
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment