C# 에서 간단한 웹 브라우징 어플을 만들때 사용하는 WebBrowser 컨트롤 이용해서
소켓을 보내는 것 보다 간단한 파징을 하실 수 있습니다.
WebBrowserReadyState.Complete 를 이용해 브라우징이 완료되길 기다리고
웹브라우저 객체의 Document 속성을 HtmlDocument 로 형 변환하시면 됩니다.
public void getCourceMenu()
{
HtmlDocument doc = null; // HtmlDocument 오브젝트
for (int i = 0; i < board.Count(); i++)
{
string url = "http://bb.unist.ac.kr/webapps/blackboard/content/courseMenu.jsp?course_id=" + board[i].url;
browser.Navigate(url); // 이동
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents(); // 웹페이지 로딩이 완료될 때 까지 대기
}
doc = browser.Document as HtmlDocument;
HtmlElementCollection options = doc.GetElementsByTagName("Option");
HtmlElement elements = ElementsByClass(doc, "courseMenu").ElementAt(0); // 아래에 정의된 함수 이용
HtmlElementCollection lists = elements.GetElementsByTagName("li"); // 태그 이름으로 파징
HtmlElementCollection a = elements.GetElementsByTagName("a");
// 파징한 데이터 활용 예
board[i].menu = new List();
board[i].menuUrl = new List();
for (int j = 0; j < lists.Count; j++)
{
board[i].menu.Add(lists[j].InnerText);
board[i].menuUrl.Add(a[j].GetAttribute("href"));
}
}
addCourseMenu();
}
static IEnumerable ElementsByClass(HtmlDocument doc, string className)
{
foreach (HtmlElement e in doc.All)
// GetAttribute 함수를 통해 클래스 이름으로 IEnumerable 오브젝트를 얻음
if (e.GetAttribute("className") == className)
yield return e; // 찾은 e 를 리턴할 IEnumerable 에 추가
}
안녕하세요 평소에 많이보고 배우는 초보 C# 개발자입니다...
답글삭제근데 이번에 새로운 프로젝트를 진행하면서 뭐좀 여쭤보고싶은점이있는데,, 혹시 도움을 좀 주실수있으시면
유선상으로 도움을 받았으면하는데 혹시 가능할가요!?