밥탄이 기록일지
CheckMate RPA C# 웹 페이지 크롤링 값 저장하기 (HtmlAgilityPack.dll) 본문
웹 사이트에서 받아온 outerHtml, InnerHtml 데이터를 가공하는 예시
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
// 파싱을 위한 HtmlAgilityPack
using HtmlAgilityPack;
public partial class CustomScript
{
public void Execute_Code()
{
// 수집해온 가격 그래프 htmlDocument로 로딩
HtmlAgilityPack.HtmlDocument myDoc = new HtmlAgilityPack.HtmlDocument();
myDoc.LoadHtml(sTmpPay);
// div의 내부 속성 값을 기준으로 커팅
HtmlAgilityPack.HtmlNodeCollection nodeCol = myDoc.DocumentNode.SelectNodes("//div[@jsname=\"jiYSXe\"]//div");
// 가격바 수 확인 = 커팅된 항목 수 확인
PrintLog(nodeCol.Count.ToString());
int count = 0;
// 가격바 수로 회전하여 내부 값 추출
foreach (HtmlAgilityPack.HtmlNode node in nodeCol)
{
// InnerText 로 데이터를 바로 수집할 수도 있지만 해당 예시의 경우에는 Text값이 없음
// Text값이 없는 경우엔 Html 값을 받아와 2차 가공이 필요함
string nodeValue = node.InnerHtml;
// 앞뒤 공백제거
nodeValue = nodeValue.Trim();
//PrintLog(nodeValue);
// 내부 값이 없는 경우는 건너띄기 = 내부값이 빈값이 아니라면 수행
if(!nodeValue.Equals(""))
{
// 내부 값에 원화표시가 있는경우 순수 금액만 결과 데이터 테이블(dtPay)에 저장한다.
if(nodeValue.Contains("₩"))
{
// 여기까지 왔을 때 nodeValue = "날짜 ₩800,000, 가격 정보입니다"
string[] tmp = nodeValue.Split('₩');
// tmp[0] = 날짜 ₩
// tmp[1] = 800,000, 가격 정보입니다
int index = tmp [1].ToString().IndexOf(", ");
// index는 ", "까지
//PrintLog(tmp [1].ToString().Substring(0, index));
// 최종 값은 800,000
nodeValue = tmp [1].ToString().Substring(0, index);
dtPay.Rows [count] [_LOOP_COUNT + 1] = nodeValue;
}else
{
PrintLog("가격 정보 없음");
dtPay.Rows [count] [_LOOP_COUNT + 1] = "가격 정보 없음";
}
count++;
}
}
}
}
소스 실행 결과
노드 가공으로 획득한 nodeValue 수에 따라 dtPay 라는 데이터 테이블에 가공이 완료된 금액 값이 입력된다.
'CheckMate RPA' 카테고리의 다른 글
CheckMate RPA C# Delay (0) | 2023.02.06 |
---|---|
CheckMate RPA C# 시간차 출력 (TimeSpan) (0) | 2023.01.19 |
CheckMate RPA C# 날짜 데이터 출력 (Datetime) (0) | 2022.12.20 |
CheckMate RPA C# PDF 여러개 분할하기 (Spire.pdf) (0) | 2022.11.22 |
CheckMate RPA C# PDF 1개 분할하기 (Spire.pdf) (0) | 2022.11.22 |