HtmlAgility:没有内容出现(C#,UWP) [英] HtmlAgility:no contents appeared (C#,UWP)

查看:209
本文介绍了HtmlAgility:没有内容出现(C#,UWP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用htmlagilitypack来解析表格,在我完成之后,我意识到我忘记证明htmlagility部分是否工作。
...
及其显然它不工作
i也不知道我错过了什么,我在哪里做了完全错误的...
导致ima初学者...
所以请别对我太难。

  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 '] /格[@ ID =' bereichaktionen']);

字符串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( //体);

if(bodyNode!= null)
{

}
}
}
}

原始网址在那里,你们可以尝试一下



首先,第三方软件包 -agility-pack.net/?z=codeplexrel =nofollow noreferrer>当前使用的Html Agility Pack 在通用应用程序中不受支持。请使用通用应用程序支持的 HtmlAgilityPack for .NET Core 1.4.9.2 。其次,方法 htmlDoc.LoadHtml(string html)的参数不是html网站的Uri,但可以从webrequest的响应中获得html内容。



所以正确的代码应该如下:

  WebRequest请求= 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 to github,你可以下载测试。


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

解决方案

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.

这篇关于HtmlAgility:没有内容出现(C#,UWP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆