问题
i tried to use htmlagilitypack to parse a table,after i ve done i realized that i forgot to prove if htmlagility part works or not. ... and its obvious it doesnt work i also didnt know what have i missed and where have i done totally wrong... cause i m a beginner... so pls dont be too hard on me.
public partial class WebForm1 : System.Net.Http.HttpClient
{
protected void Page_Load(object sender, EventArgs e)
{
System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
string header = "ie";
if (!headers.UserAgent.TryParseAdd(header))
{
throw new Exception("Invalid header value: " + header);
}
header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
if (!headers.UserAgent.TryParseAdd(header))
{
throw new Exception("Invalid header value: " + header);
}
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(" http://www.eurogymnasium-waldenburg.de/egw_content/Stunden_Vertretungsplan/home.html");
HtmlNode docNodes = htmlDoc.DocumentNode;
HtmlNode navNode = htmlDoc.GetElementbyId("bereichaktionen");
HtmlNode docNode = htmlDoc.DocumentNode.SelectSingleNode("/html/body[@class='ui-widget']/div[@id='main']/div[@id='vplan']/div[@id='bereichaktionen']");
string nodeValue;
nodeValue = (docNode.InnerText);
Debug.WriteLine("nodeValue");
// i doubt theres somethin wrong above but im not sure whats wrong.
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
{
}
else
{
if (htmlDoc.DocumentNode != null)
{
HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");
if (bodyNode != null)
{
}
}
}
}
the origin url is there ,u guys could have a try
Thank y'all X.L
回答1:
Firstly, the third party package Html Agility Pack you are using currently is not supported in universal app. Please use HtmlAgilityPack for .NET Core 1.4.9.2 which is supported in universal app.
Secondly, the parameter of the method htmlDoc.LoadHtml(string html)
is not the Uri of html site, but the html content which can be got from a webrequest's response.
So the right code should be as followings:
WebRequest request = HttpWebRequest.Create("http://www.eurogymnasium-waldenburg.de/egw_content/Stunden_Vertretungsplan/home.html");
WebResponse response = await request.GetResponseAsync();
Stream stream = response.GetResponseStream();
var result = "";
using (StreamReader sr = new StreamReader(stream))
{
result = sr.ReadToEnd();
}
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(result);
var node = htmlDoc.DocumentNode.SelectSingleNode("/html/body[@class='ui-widget']/div[@id='main']/div[@id='vplan']/div[@id='bereichaktionen']");
And I also upload the complete project CHtmlAgility to github you can download for testing.
回答2:
HtmlAgilityPack for UWP (also WinRT and other similar tech) doesn't support XPath. Answer from the man behind HtmlAgilityPack himself https://stackoverflow.com/a/15941723/5562523
The Html Agility Pack relies on .NET for the XPATH implementation. Unfortunately, WinRT doesn't support XPATH, so you don't have anything related to XPATH in Html Agility Pack for WinRT.
来源:https://stackoverflow.com/questions/37896836/htmlagilityno-contents-appeared-c-uwp