오늘 처음 기업과 슬랙으로 소통을 하였다.
둑은둑은!!
나에게 우선 주어진 미션은 fmkorea
를 크롤링 하는 함수를 짜보는 것이었다.
해당 미션을 수행하기 위해서
Cheerio
라는 모듈을 알려주어서 사용해 보고 있다.
Cheerio의 예시
const cheerio = require("cheerio"); const request = require("requestretry"); let query = encodeURI("query=피자"); const options = { method: "GET", url: `https://search.naver.com/search.naver?${query}`, }; const getHtml = async () => { try { let html = await request(options); const $ = cheerio.load(html.body); const $tits = $(".related_srch li .tit"); let arr = $tits .toArray() .reduce((acc, it, idx, src) => [...acc, it.children[0].data], []); console.log(arr); } catch (error) { console.error(error); } }; getHtml();
제이쿼리를 사용해본적은 없지만 제이쿼리와 비슷한 문법을 갖고 있다고 한다.
$(query)
로 DOM 객체를 지정해서 다룰 수 있다는 장점이 있다.
const { getViews } = require("./actions");
const { request, setOption } = require("./help");
const options = setOption("애플워치", true);
const getHtml = async () => {
try {
const $ = await request(options);
const $titles = $("a.hotdeal_var8")
.toArray()
.map((el) => $(el).text().trim());
const $hrefs = $("a.hotdeal_var8")
.toArray()
.map((el) => $(el).attr("href"));
const $dates = $("span.regdate")
.toArray()
.map((el) => $(el).text().trim());
//authors의 모음
const $authors = $("span.author")
.toArray()
.map((el) => $(el).text().slice(3));
const views = await getViews($, $hrefs);
// 작성일 모음
} catch (error) {
console.error(error);
}
};
getHtml();
때문에 깜짝놀랐다. 결국 핸드폰으로 핫스팟 연결을 통해 코드를 치게 되었다.
구글링을 해본 결과
이런 식으로!
{ method: "GET", headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0", }, url: url, };
내가 크롤러를 작성하고 있는 fm 코리아의 경우 그나마 24시간 블락은 되지 않는다.
내게 닥친 시련은 한 번에 15개 정도? 요청을 했을 때 IP 차단 때문에 정보가 불러와지지 않는다는 것!
(href를 타고 들어가 정보를 뽑고, 나오는 과정을 한번에 15번 이상 반복했을 때!)
내일 해결 방법을 찾아봐야겠다.