簡體   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