Toggle navigation
Toggle navigation
This project
Loading...
Sign in
정나리
/
Jaksimsamil
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
송용우
2020-06-12 16:54:22 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
89c2058325daef9176c85b838f79038c20c70655
89c20583
2 parents
6be6c2a9
eb044ee7
Merge branch 'feature/crawling' into develop
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
20 deletions
jaksimsamil-server/src/util/StringToDate.js
jaksimsamil-server/src/util/getBJ.js
jaksimsamil-server/src/util/StringToDate.js
0 → 100644
View file @
89c2058
exports
.
StringToDate_BJ
=
function
(
date_str
)
{
let
arr_date
=
date_str
.
split
(
" "
);
//yyyy m dd tt MM SS Fomat LIST
let
arr_date_r
=
arr_date
.
map
(
function
(
str
)
{
let
str_r
=
str
.
slice
(
0
,
-
1
);
return
str_r
.
length
==
1
?
"0"
+
str_r
:
str_r
;
});
return
arr_date_r
[
0
]
+
arr_date_r
[
1
]
+
arr_date_r
[
2
];
//YYYYMMDD 형식으로 반환
};
jaksimsamil-server/src/util/getBJ.js
View file @
89c2058
const
axios
=
require
(
"axios"
);
const
cheerio
=
require
(
"cheerio"
);
let
userid_test
=
"syw5141"
;
const
getHtml
=
async
(
userid
)
=>
{
const
StringToDate
=
require
(
"./StringToDate"
);
/*
ToDO
- 유저 네임 검증
- 예외 처리
*/
exports
.
getBJ
=
async
function
(
userid
)
{
let
data_list
=
[];
let
next_page_link
=
""
;
await
getStartPage
(
userid
).
then
((
html
)
=>
{
//시작 페이지를 가져온다.
//같은 객체를 두번 선언한다. 퍼포먼스에 문제 생길수도
//함수에 객체를 넘기는 방법도 있다.
//첫 페이지 가져온다.
data_list
.
push
(
getData
(
html
));
next_page_link
=
getNextPageLink
(
html
);
});
while
(
next_page_link
!=
-
1
)
{
//다음 페이지를 가져온다.
await
getNextPage
(
next_page_link
).
then
((
html
)
=>
{
data_list
.
push
(
getData
(
html
));
next_page_link
=
getNextPageLink
(
html
);
});
}
return
data_list
.
flat
(
1
);
};
const
getStartPage
=
async
(
userid
)
=>
{
//유저 아이디 입력
try
{
return
await
axios
.
get
(
"https://www.acmicpc.net/user/"
+
userid
);
return
await
axios
.
get
(
"https://www.acmicpc.net/status?user_id="
+
userid
+
"&result_id=4"
);
}
catch
(
error
)
{
console
.
log
(
error
);
}
};
getHtml
(
userid_test
).
then
((
html
)
=>
{
let
psList
=
[];
const
$
=
cheerio
.
load
(
html
.
data
);
const
$bodyList
=
$
(
"div.panel-body"
).
children
();
const
getNextPage
=
async
(
link
)
=>
{
//링크 입력
try
{
return
await
axios
.
get
(
link
);
}
catch
(
error
)
{
console
.
log
(
error
);
}
};
$bodyList
.
each
(
function
(
i
)
{
if
(
i
%
2
==
0
)
{
psList
[
i
/
2
]
=
{
problem_number
:
$
(
this
).
children
().
text
(),
problem_title
:
$
(
this
).
next
().
children
().
text
(),
};
}
const
getData
=
(
html
)
=>
{
//페이지 데이터 파싱
let
psArr
=
[];
const
$
=
cheerio
.
load
(
html
.
data
);
const
$bodyList
=
$
(
"#status-table > tbody"
);
$bodyList
.
children
().
each
((
index
,
element
)
=>
{
psArr
.
push
({
problem_number
:
$
(
element
).
find
(
"a.problem_title"
).
text
(),
problem_title
:
$
(
element
).
find
(
"a.problem_title"
).
attr
(
"title"
),
solved_date
:
StringToDate
.
StringToDate_BJ
(
$
(
element
).
find
(
"a.real-time-update"
).
attr
(
"title"
)
),
});
});
console
.
log
(
psList
);
return
psList
;
});
//body > div.wrapper > div.container.content > div.row > div:nth-child(2) > div:nth-child(3) > div.col-md-9 > div:nth-child(1) > div.panel-body
return
psArr
;
};
const
getNextPageLink
=
(
html
)
=>
{
//다음 페이지가 있으면 다음 페이지 주소 return, 없으면 -1 return
const
$
=
cheerio
.
load
(
html
.
data
);
return
$
(
"#next_page"
).
attr
(
"href"
)
?
"https://www.acmicpc.net/"
+
$
(
"#next_page"
).
attr
(
"href"
)
:
-
1
;
};
...
...
Please
register
or
login
to post a comment