Scraping && Crawling
- 웹 사이트 혹은 특정한 시스템에서 특정 정보를 추출 및 가공을 하여 사용하는 소프트웨어 기술
- HTTP 통신은 API를 요청하여 문자열로 된 html 및 데이터 (ex:json)을 받아오는 통신이다, 이점을 이용하여 문자열데이터를 받아와 활용하는 기술이다.
Scraping
- Header 태그안 og: property를 가진 태그를 긁어온다.
- 검색시에, 검색어키워드에 관한 미리보기에 사용된다.
- Cors 허용이 되어 있어야 스크래핑이 가능하다.
Scraping 사용방법
export async function getOgTag(url) {
if (url.match("http://" || "https://") === null) {
url = "http://" + url;
}
console.log(url);
const html = await axios.get(url);
const $ = cheerio.load(html.data);
let value = [];
$("meta").each((i, el) => {
if ($(el).attr("property")?.includes("og:")) {
value.push($(el).attr("content"));
}
});
Crawling
- 특정 데이터를 추출하기 위해 사용한다. ex) 주식데이터, 비즈니스 데이터
- 크로미움 : 크롬의 원래 버전
Crawling
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 720 });
await page.goto(url);
await page.waitForTimeout(1000);
let nameList = [];
let imgList = [];
for (let i = 1; i <= 10; i++) {
nameList.push(
await page.$eval(
`#container > div.content > div.product_result_wrap.product_result_wrap01 > div > dl > dd:nth-child(2) > div.product_list > dl > dd:nth-child(2) > ul > li:nth-child(${i}) > dl > dd`,
(el) => el.textContent
)
);
imgList.push(
await page.$eval(
`#container > div.content > div.product_result_wrap.product_result_wrap01 > div > dl > dd:nth-child(2) > div.product_list > dl > dd:nth-child(2) > ul > li:nth-child(${i}) > dl > dt > a > img`,
(el) => el.getAttribute("src")
)
);
}
browser.close();