繁体   English   中英

HtmlAgility:没有内容出现(C#,UWP)

[英]HtmlAgility:no contents appeared (C#,UWP)

我尝试使用htmlagilitypack解析表后,我意识到我忘了证明htmlagility部分是否起作用。 ...而且很明显它不起作用,我也不知道我错过了什么,我在哪里做的完全错了...造成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']/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)
                {

                }
            }
        }
    }

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

感谢y'all XL

首先,通用应用程序不支持您当前使用的第三方软件包Html Agility Pack 针对通用应用程序支持的.NET Core 1.4.9.2使用HtmlAgilityPack

其次, 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,您可以下载进行测试。

用于UWP的HtmlAgilityPack(也包括WinRT和其他类似技术)不支持XPath。 HtmlAgilityPack背后的人自己回答https://stackoverflow.com/a/15941723/5562523

Html Agility Pack依靠.NET来实现XPATH。 不幸的是,WinRT不支持XPATH,因此您在WinRT的HTML Agility Pack中没有与XPATH相关的任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM