Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- ESP-01WiFi
- scroll-snap
- CSS Flex
- 이친수문제
- 2193
- peap
- 백준15988풀이
- Flexible box
- reactNative
- 프로젝트초기설정
- @supports
- ESP8266WiFi
- 아두이노 우노
- 백준java
- aspect-ratio
- 포인터
- dp문제
- 노마드코더
- ESP-01
- 백준문제풀이 #백준 #백준문제 #스타트택시
- 연결리스트
- ESP8266
- CSS
- 백준
- C
- 백준 #백준2661 #좋은수열 #Java #코딩
- 리액트네이티브
- 백준풀이
- scss
- 백준자바
Archives
- Today
- Total
코딩 농장
[Axios/cheerio/Node.js] 깃허브 잔디(커밋 횟수) 크롤링하기 본문
728x90
이번에 크롤링을 해볼 수 있었씀다!
이거 자꾸 안 되어서 조금 화나던 찰나에.. 어떤 분 블로그에서 python으로 하신 것을 참고해서 코딩을 하게 되었는데...
그 블로그를 못 찾겠따....!!! 찾게 되면 여기에 링크를 걸어둘게요.
일단 제 코드는 다음과 같습니다!
const axios = require('axios');
const cheerio = require('cheerio');
const gitData = require('../models/gitDataModel');
const User = require('../models/userModel');
/*
getDate()
result에 2021년부터 현재년도까지 담음.
이후 crawlingModule 에서 result 배열에 있는 년도 git 데이터를 다 크롤링함!
*/
const getDate = () => {
const date = new Date();
let result = [];
var yeargap = date.getFullYear()-2021;
for(let i = 0; i<=yeargap; i++){
result.push(2021+i);
}
return result;
}
/*
crawlingModule(gitId)
axios와 cheerio를 이용하여 깃허브 사이트 크롤링!
props로 받은 gitId를 이용함.
result = [
{ date: '2021-01-01', count: '0' },
{ date: '2021-01-02', count: '0' },
{ date: '2021-01-03', count: '0' },
...
]
*/
const crawlingModule = async (gitId) => {
let result = [];
const to = getDate();
for(let i=0; i<to.length; i++){
await axios.get(`https://github.com/${gitId}?tab=overview&from=${to[i]}-01-01&to=${to[i]}-12-31`)
.then(html => {
$ = cheerio.load(html.data);
const crawl = $('svg > g > g > rect');
for(let i =0; i<Object.keys(crawl).length; i++){
if(crawl[i] === undefined) break;
result.push({
date: crawl[i].attribs['data-date'],
count: crawl[i].attribs['data-count']
})
}
})
.catch((e) => {
console.log("error");
console.log(e);
})
}
return result;
};
axios로 페이지 연결!
그리고 잔디의 경로인 svg > g > g > rect를 크롤링!
그러면 주석에 나와있는 결과대로 잘 나옵니다
저는 from to 를 썼는데 저부분 없애도 잘 동작해요 ><
이거 삽질 진짜 엄청 했는데 코드만 보면 간단해보이기도 하고.. ㅠ
'프로젝트 > 1Challenge' 카테고리의 다른 글
heroku에 배포 중인 브랜치가 삭제되었을 때 해결법 (0) | 2021.08.05 |
---|---|
[Nodejs/jwt] 배포 후, 로그인 쿠키 설정이 안 된다면? res.cookie가 안 될 때, samesite? (0) | 2021.07.05 |
[Heroku/Nodejs] Nodejs Heroku로 배포하기 - 설정법 (0) | 2021.07.05 |
[React/Node.js/scss] 프로젝트 레포지토리 초기 설정과 폴더 구조 (0) | 2021.03.27 |
Comments