我無法弄清楚如何使用HTML敏捷包遍歷DOM。使用HTML敏捷包遍歷DOM
例如,假設我想找到一個含有id="gbqfsa"
的元素。
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(Url);
var foo = from bar in doc.DocumentNode.DescendantNodes()
where bar.Attributes["id"].Value == "gbqfsa"
select bar.InnerText;
現在我這樣做(上圖),但foo
是走出來的null
。我究竟做錯了什麼?
編輯:這是我使用的if
聲明。我只是測試,看看InnerText
元素是否等於「Google搜索」。
if (foo.Equals("Google Search"))
{
HasSucceeded = 1;
MessageBox.Show(yay);
}
else
{
MessageBox.Show("kms");
}
return HasSucceeded;
如果執行var'foo = ...'行,'foo'不能爲'null'。它可以是一個空集合,但不是'null'。 –
好吧,當我逐句通過代碼說它是空的。任何想法我做錯了,但? –
你能舉一個不起作用的URL和元素ID的例子嗎? –