我尝试使用 htmlagilitypack 来解析表格,完成后我意识到我忘记证明 htmlagility 部分是否有效。 ... 很明显它不起作用 我也不知道我错过了什么,我在哪里做错了...... 因为我是初学者... 所以请不要对我太苛刻。
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");
//我怀疑上面有什么问题,但我不确定是什么问题。
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
{
}
else
{
if (htmlDoc.DocumentNode != null)
{
HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");
if (bodyNode != null)
{
}
}
}
}
有源地址,大家可以试试
谢谢大家 X.L
最佳答案
首先是第三方包Html Agility Pack通用应用程序不支持您当前使用的。请使用HtmlAgilityPack for .NET Core 1.4.9.2 通用应用程序支持。
其次,方法htmlDoc.LoadHtml(string html)
的参数不是html站点的Uri,而是可以从webrequest的响应中得到的html内容。
所以正确的代码应该是这样的:
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']");
我还上传了完整的项目CHtmlAgility到github你可以下载进行测试。
关于c# - HtmlAgility :no contents appeared (C#, UWP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37896836/