Last active
June 13, 2020 02:40
-
-
Save hmmhmmhm/2205f95eb8e54c1745344c10b9e2fbce to your computer and use it in GitHub Desktop.
JSDOM 을 이용한 간단한 ssr 페이지 크롤링 예시
This file contains hidden or 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
| import axios from 'axios' | |
| import { JSDOM } from 'jsdom' | |
| export const crawling = async (url: string) => { | |
| try { | |
| const response = await axios.get(url) | |
| const { window } = new JSDOM(response.data) | |
| // * 크롤링 코드 시작 | |
| const iframePath = window.document.querySelector('#contentarea_left > iframe').src | |
| if (!iframePath) throw new Error() | |
| const timeAndIsOpenedData = window.document.querySelector('#time').textContent.trim().split(' ') | |
| if (!timeAndIsOpenedData) throw new Error() | |
| const currentDate = timeAndIsOpenedData[0].split('.').join('-') | |
| const isOpen = timeAndIsOpenedData[1] != '닫힘' | |
| // * 크롤링 코드 완료 | |
| return { | |
| innerURL: `${url}/${iframePath}`, | |
| currentDate, | |
| isOpen, | |
| } | |
| } catch (e) { | |
| return undefined | |
| } | |
| } | |
| crawling('https://~') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment