웹 스크래퍼 응용 프로그램을 만들고 싶습니다. 웹 브라우저 컨트롤, htmlagilitypack 및 xpath로 수행하고 싶습니다.
지금은 xpath 생성기 (이 목적으로 webbrowser를 사용했습니다)를 만들 수 있었지만 제대로 작동하지만 때로는 자바 스크립트 또는 아약스를 통해 생성 된 콘텐츠를 동적으로 가져올 수 없습니다. 또한 웹 브라우저 컨트롤 (실제로 IE 브라우저)이 "tbody"와 같은 추가 태그를 생성하는 동안 htmlagilitypack`htmlWeb.Load (webBrowser.DocumentStream);`이 보이지 않는 것을 알았습니다.
또 다른 메모. 다음 코드는 실제로 현재 웹 페이지 소스를 얻는다는 것을 알았지 만 htmlagilitypack`(mshtml.IHTMLDocument3) webBrowser.Document.DomDocument;
도와주세요?
방금 HtmlAgilityPack에 웹 페이지에서 일부 아약스 동적 콘텐츠를 렌더링하려고 시간을 보냈다.이 웹 페이지를 발견 할 때까지 쓸모없는 게시물을 다른 게시물로 이동하려고했다.
대답은 초기 게시물 아래의 주석에 숨겨져 있으며 나는 그것을 바로 잡아야한다고 생각했습니다.
이것은 처음에 사용하고 작동하지 않는 방법입니다.
private void LoadTraditionalWay(String url)
{
WebRequest myWebRequest = WebRequest.Create(url);
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
TextReader reader = new StreamReader(ReceiveStream, encode);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(reader);
reader.Close();
}
WebRequest는 누락 된 내용을 렌더링하는 ajax 쿼리를 렌더링하거나 실행하지 않습니다.
이것이 효과가있는 해결책입니다.
private void LoadHtmlWithBrowser(String url)
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate(url);
waitTillLoad(this.webBrowser1);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)webBrowser1.Document.DomDocument;
StringReader sr = new StringReader(documentAsIHtmlDocument3.documentElement.outerHTML);
doc.Load(sr);
}
private void waitTillLoad(WebBrowser webBrControl)
{
WebBrowserReadyState loadStatus;
int waittime = 100000;
int counter = 0;
while (true)
{
loadStatus = webBrControl.ReadyState;
Application.DoEvents();
if ((counter > waittime) || (loadStatus == WebBrowserReadyState.Uninitialized) || (loadStatus == WebBrowserReadyState.Loading) || (loadStatus == WebBrowserReadyState.Interactive))
{
break;
}
counter++;
}
counter = 0;
while (true)
{
loadStatus = webBrControl.ReadyState;
Application.DoEvents();
if (loadStatus == WebBrowserReadyState.Complete && webBrControl.IsBusy != true)
{
break;
}
counter++;
}
}
아이디어는 ajax 콘텐츠를 렌더링 할 수있는 WebBrowser를 사용하여로드 한 다음 페이지가 완전히 렌더링 될 때까지 기다린 다음 Microsoft.mshtml 라이브러리를 사용하여 HTML을 민첩성 팩으로 다시 구문 분석하는 것입니다.
이것은 동적 데이터에 액세스 할 수있는 유일한 방법이었습니다.
누군가가 도움이되기를 바랍니다.