簡體   English   中英

使用HtmlAgilityPack解析C#中的網頁信息

[英]using HtmlAgilityPack for parsing a web page information in C#

我正在嘗試使用HtmlAgilityPack來解析網頁信息。 這是我的代碼:

using System;
using HtmlAgilityPack;

namespace htmparsing
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string url = "https://bugs.eclipse.org";
            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load(url);
            foreach(HtmlNode node in doc){
                //do something here with "node"
            }               
        }
    }
}

但是當我嘗試訪問doc.DocumentElement.SelectNodes我無法在列表中看到DocumentElement 我在引用中添加了HtmlAgilityPack.dll,但我不知道是什么問題。

我有一篇文章演示了使用ASP.NET使用HAP(HTML Agility Pack)抓取DOM元素。 它只是讓您逐步完成整個過程。 你可以看看並嘗試一下。

在ASP.NET中使用HtmlAgilityPack(HAP)刮取HTML DOM元素

關於你的過程它對我來說很好。 我嘗試過這種方式就像你做了一次改變一樣。

string url = "https://www.google.com";
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(url);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a")) 
{
    outputLabel.Text += node.InnerHtml;
}

得到了預期的輸出。 問題是你是從的HTMLDocument對象實際上應該是DocumentNode要求DocumentElement。 以下是HTMLAgilityPack開發人員對您遇到的問題的回復

HTMLDocument.DocumentElement不在對象瀏覽器中

你看到的行為是正確的。

看看你實際在做什么: http//htmlagilitypack.codeplex.com/SourceControl/latest#Release/1_4_0/HtmlAgilityPack/HtmlNode.cs

您要求頂部元素選擇與某個xpath匹配的節點。 除非你的xpath表達式以//開頭,否則你要求它為相關節點,它們是后代節點。 文檔元素本身不是后代,因為沒有元素是其自身的后代。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM