Toggle navigation
Toggle navigation
This project
Loading...
Sign in
박건희
/
emon_bot
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
String15
2022-05-26 13:52:04 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c6c3ae6e50925baa67984e880ce1fdd5fa5c7611
c6c3ae6e
1 parent
cda9f20b
Update crawling method to search 1->n
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
29 deletions
Commands/Stationery/search.js
Commands/Stationery/search.js
View file @
c6c3ae6
...
...
@@ -2,8 +2,8 @@ const Discord = require("discord.js");
exports
.
run
=
async
(
client
,
msg
,
args
,
prefix
)
=>
{
if
(
args
[
0
])
{
// 명령어 뒤에 입력값이 있을 경우 (ex. !<명령어> <채팅>)
// 검색어 한 문장으로 합치기
const
search
=
args
.
join
(
' '
)
// console.log(search)
const
search
=
args
.
join
(
' '
)
;
// console.log(search)
;
// 각 사이트별 제품 검색
const
puppeteer
=
require
(
'puppeteer'
);
//include Puppeteer Library
...
...
@@ -13,70 +13,98 @@ exports.run = async (client, msg, args, prefix) => {
console
.
log
(
'Open Browser'
);
// 1. bestpen
await
page
.
goto
(
'http://www.bestpen.kr/html/mainm.html'
);
console
.
log
(
"bestpen crawling"
);
await
page
.
goto
(
'http://www.bestpen.kr/index.html'
);
// 검색창으로 이동 & search 검색
await
page
.
click
(
'#header > div.headerBtm > div > p.searchOpen > i'
)
await
page
.
type
(
'#keyword'
,
search
)
await
page
.
click
(
'#header > div.headerBtm > div > p.searchOpen > i'
)
;
await
page
.
type
(
'#keyword'
,
search
)
;
await
page
.
keyboard
.
press
(
"Enter"
);
// await page.screenshot({ path : "screenshot.png" })
// await page.screenshot({ path : "screenshot.png" })
;
// 검색 결과 가져오기
// 검색 결과 가져오기 (최대 4개)
var
bestpen
=
''
;
for
(
var
i
=
1
;
i
<=
4
;
i
++
)
{
try
{
var
title
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(1
) > dd > ul > li.prd-name > a'
);
var
title
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('
+
i
+
'
) > dd > ul > li.prd-name > a'
);
var
bestpen_title
=
await
page
.
evaluate
(
title
=>
title
.
textContent
,
title
);
// console.log("베스트펜 검색 결과 제품명 : ", bestpen_title);
var
link
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(1
) > dt > a'
);
var
link
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('
+
i
+
'
) > dt > a'
);
var
bestpen_link
=
await
page
.
evaluate
(
link
=>
link
.
href
,
link
);
// console.log("베스트펜 검색 링크 : ", bestpen_link);
try
{
var
price
=
await
page
.
waitForSelector
(
"#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(1) > dd > ul > li.prd-price > p:nth-child(2) > span.price"
);
var
price
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('
+
i
+
') > dd > ul > li.prd-price > p:nth-child(2) > span.price'
);
var
bestpen_data
=
await
page
.
evaluate
(
price
=>
price
.
textContent
,
price
);
// console.log("베스트펜 검색 결과 가격 : ", bestpen_data);
}
catch
{
// 품절이라서 금액정보가 없을 경우 "SOLD OUT"으로 표시
bestpen_data
=
"SOLD OUT"
bestpen_data
=
"SOLD OUT"
;
}
bestpen
+=
`[
${
bestpen_title
}
](
${
bestpen_link
}
) -
${
bestpen_data
}
`
+
'\n'
}
catch
{
if
(
i
==
1
)
{
// 제품 정보가 아예 없을 경우 "검색결과 없음"으로 표시
bestpen_title
=
"검색결과 없음"
bestpen_link
=
""
bestpen_data
=
""
bestpen_title
=
"검색결과 없음"
;
bestpen_link
=
""
;
bestpen_data
=
""
;
bestpen
+=
`[
${
bestpen_title
}
](
${
bestpen_link
}
) -
${
bestpen_data
}
`
+
'\n'
break
;
}
else
{
// 제품 개수가 4개 이하인 경우
break
;
}
}
}
// 2. 펜카페
console
.
log
(
"pencafe crawling"
);
await
page
.
goto
(
'http://www.pencafe.co.kr/'
);
// search 검색
await
page
.
type
(
'#header > div.hd_mib > div.hd_sch.f_left > form > fieldset > input'
,
search
)
await
page
.
type
(
'#header > div.hd_mib > div.hd_sch.f_left > form > fieldset > input'
,
search
)
;
await
page
.
keyboard
.
press
(
"Enter"
);
// await page.screenshot({ path : "screenshot.png" })
// await page.screenshot({ path : "screenshot.png" })
;
// 검색 결과 가져오기
// 검색 결과 가져오기 (최대 4개)
var
pencafe
=
''
;
for
(
var
i
=
1
;
i
<=
4
;
i
++
)
{
try
{
var
title
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(1
) > dd > ul > li.prd-name > a'
);
var
title
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('
+
i
+
'
) > dd > ul > li.prd-name > a'
);
var
pencafe_title
=
await
page
.
evaluate
(
title
=>
title
.
textContent
,
title
);
console
.
log
(
"펜카페 검색 결과 제품명 : "
,
pencafe_title
);
//
console.log("펜카페 검색 결과 제품명 : ", pencafe_title);
var
link
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(1
) > dd > ul > li.prd-name > a'
);
var
link
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('
+
i
+
'
) > dd > ul > li.prd-name > a'
);
var
pencafe_link
=
await
page
.
evaluate
(
link
=>
link
.
href
,
link
);
console
.
log
(
"펜카페 검색 링크 : "
,
pencafe_link
);
//
console.log("펜카페 검색 링크 : ", pencafe_link);
try
{
var
price
=
await
page
.
waitForSelector
(
"#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child(1) > dd > ul > li.prd-price > span"
);
var
price
=
await
page
.
waitForSelector
(
'#searchWrap > div > div.item-wrap > div:nth-child(2) > dl:nth-child('
+
i
+
') > dd > ul > li.prd-price > span'
);
var
pencafe_data
=
await
page
.
evaluate
(
price
=>
price
.
textContent
,
price
);
console
.
log
(
"펜카페 검색 결과 가격 : "
,
pencafe_data
);
//
console.log("펜카페 검색 결과 가격 : ", pencafe_data);
}
catch
{
// 품절이라서 금액정보가 없을 경우 "SOLD OUT"으로 표시
pencafe_data
=
"SOLD OUT"
pencafe_data
=
"SOLD OUT"
;
}
pencafe
+=
`[
${
pencafe_title
}
](
${
pencafe_link
}
) -
${
pencafe_data
}
`
+
'\n'
}
catch
{
if
(
i
==
1
)
{
// 제품 정보가 아예 없을 경우 "검색결과 없음"으로 표시
pencafe_title
=
"검색결과 없음"
pencafe_link
=
""
pencafe_data
=
""
pencafe_title
=
"검색결과 없음"
;
pencafe_link
=
""
;
pencafe_data
=
""
;
pencafe
+=
`[
${
pencafe_title
}
](
${
pencafe_link
}
) -
${
pencafe_data
}
`
+
'\n'
break
;
}
else
{
// 제품 개수가 4개 이하인 경우
break
;
}
}
}
// 브라우저 닫기
...
...
@@ -88,9 +116,9 @@ exports.run = async (client, msg, args, prefix) => {
.
setTitle
(
`
${
search
}
에 대한 검색 결과`
)
.
setColor
(
"E5D49A"
)
// 베스트펜 검색 결과 (ex. 제품명(링크) - 금액)
.
addField
(
'베스트펜'
,
`
[
${
bestpen_title
}
](
${
bestpen_link
}
) -
${
bestpen_data
}
`
)
.
addField
(
'베스트펜'
,
`
${
bestpen
}
`
)
// 펜카페 검색 결과
.
addField
(
'펜카페'
,
`
[
${
pencafe_title
}
](
${
pencafe_link
}
) -
${
pencafe_data
}
`
)
.
addField
(
'펜카페'
,
`
${
pencafe
}
`
)
msg
.
reply
({
embeds
:
[
Commands
]
});
});
}
else
{
...
...
Please
register
or
login
to post a comment