클래스 응답 :
public string WebResponse(string url) //class through which i'll have link of website and will parse some divs in method of this class
{
string html = string.Empty;
try
{
HtmlDocument doc = new HtmlDocument(); //when code comes here it gives an error htmldocument.cs not found,and open window for browsing source
WebClient client = new WebClient(); // even if i put htmlWeb there it still look for HtmlWeb.cs not found
html = client.DownloadString(url); //is this from some breakpoint error coz i set only one in method where i am parsing,
doc.LoadHtml(html);
}
catch (Exception)
{
html = string.Empty;
}
return html; //please help me to remove this error using html agility pack with console application
}
비록 내가 새 프로젝트를 만들고 여기에 붙어있는 코드를 실행해도 DLL을 추가했다하더라도이 오류는 나에게이 오류를 제거하도록 도와 준다.
그 오류는 때때로 Nuget html 민첩성 팩을 사용하고 있기 때문에 Visual Studio 갤러리에서 너겟을 업데이트 한 다음 html 민첩성 팩을 설치하고 프로젝트에서 실행하십시오.
WebResponse는 추상 클래스로서 예약어로 사용됩니다. 둘째 - WebResponse를 사용하려면 클래스가 WebResponse 즉, WebResponse에서 상속해야합니다.
public class WR : WebResponse
{
//Code
}
또한. 현재 코드는 Html Agility Pack과 아무런 관련이 없습니다. 웹 페이지의 HTML을 HtmlDocument에로드하려면 다음을 수행하십시오.
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
try{
var temp = new Uri(url);
var request = (HttpWebRequest)WebRequest.Create(temp);
request.Method = "GET";
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
htmlDoc.Load(stream, Encoding.GetEncoding("iso-8859-9"));
}
}
}catch(WebException ex){
Console.WriteLine(ex.Message);
}
그런 다음 Html 문서에서 노드를 가져 오려면 xPath를 다음과 같이 사용해야합니다.
HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//body");
Console.WriteLine(node.InnerText);