npm install axios cheerio iconv-lite -s
const cheerio = require("cheerio");
const axios = require("axios");
const iconv = require("iconv-lite");
const url = "http://???????";
router.get("/??????", async (req, res) => {
try {
await axios({
url: url,
method: "GET",
responseType: "arraybuffer",
}).then(async (html) => {
const content = iconv.decode(html.data, "EUC-KR").toString();
// EUR-KR part is different from pages.
});
res.send({ result: "success", message: "크롤링이 완료 되었습니다." });
} catch (error) {
console.log(error)
res.send({ result: "fail", message: "크롤링에 문제가 발생했습니다", error:error });
}
});
↓
router.get("/goods/add/crawling", async (req, res) => {
try {
await axios({
url: url,
method: "GET",
responseType: "arraybuffer",
}).then(async (html) => {
const content = iconv.decode(html.data, "EUC-KR").toString();
const $ = cheerio.load(content);
const list = $("ol li");
await list.each( async (i, tag) => {
let desc = $(tag).find("p.copy a").text()
let image = $(tag).find("p.image a img").attr("src")
let title = $(tag).find("p.image a img").attr("alt")
let price = $(tag).find("p.price strong").text();
console.log(desc, image, title, price);
})
});
res.send({ result: "success", message: "크롤링이 완료 되었습니다." });
} catch (error) {
console.log(error)
res.send({ result: "fail", message: "크롤링에 문제가 발생했습니다", error:error });
}
});