對於初學者,請提前致謝!HtmlAgilityPack並未從網頁獲取所有html代碼/文本
我能夠從網頁中提取一段代碼,看起來與以下代碼塊類似。
<div id="playerStats">
<div id="hp"><span class="title">HP:</span></div>
<div id="mp"><span class="title">MP:</span></div>
<div id="magicResist"><span class="title">Magic Resist</span></div>
<div id="physicalDefend"><span class="title">Physical Defence</span></div>
<div id="phyCriticalReduceRate"><span class="title">Strike Resist</span></div>
<div id="phyCriticalDamageReduce"><span class="title">Strike fortitude</span></div>
<div id="physicalRight"><span class="title">Main Hand Attack</span></div>
<div id="accuracyRight"><span class="title">Main Hand Accuracy</span></div>
<div id="criticalRight"><span class="title">Main Hand Critical</span></div>
<div id="physicalLeft"><span class="title">Off Hand Attack</span></div>
<div id="accuracyLeft"><span class="title">Off Hand Accuracy</span></div>
<div id="criticalLeft"><span class="title">Off Hand Critical</span></div>
<div id="attackSpeed"><span class="title">Attack Speed</span></div>
<div id="magicalBoost"><span class="title">Magic Boost</span></div>
<div id="magicalAccuracy"><span class="title">Magic Accuracy</span></div>
<div id="magicalCriticalRight"><span class="title">Crit Spell</span></div>
<div id="castingTimeRatio"><span class="title">Casting Speed</span></div>
<div id="block"><span class="title">Block</span></div>
<div id="dodge"><span class="title">Evasion</span></div>
</div>
從
以下URI for this character statistics page of a video game. (And you should clearly see the table of stats in the middle of the page.) 如果使用類似谷歌Chrome的F-12瀏覽器的功能來查看HTML源代碼,你會發現在/跨度和/ DIV類似以下內容之間有值代碼:
<div id="playerStats">
<div id="hp"><span class="title">HP:</span>"12213"</div>
<div id="mp"><span class="title">MP:</span>"4000"</div>
<div id="magicResist"><span class="title">Magic Resist</span>"4618"</div>
<div id="physicalDefend"><span class="title">Physical Defence</span>"1725"</div>
<div id="phyCriticalReduceRate"><span class="title">Strike Resist</span>"1518"</div>
<div id="phyCriticalDamageReduce"><span class="title">Strike fortitude</span>"392"</div>
<div id="physicalRight"><span class="title">Main Hand Attack</span>"201"</div>
<div id="accuracyRight"><span class="title">Main Hand Accuracy</span>"201"</div>
<div id="criticalRight"><span class="title">Main Hand Critical</span>"201"</div>
<div id="physicalLeft"><span class="title">Off Hand Attack</span>"201"</div>
<div id="accuracyLeft"><span class="title">Off Hand Accuracy</span>"201"</div>
<div id="criticalLeft"><span class="title">Off Hand Critical</span>"201"</div>
<div id="attackSpeed"><span class="title">Attack Speed</span>"201"</div>
<div id="magicalBoost"><span class="title">Magic Boost</span>"201"</div>
<div id="magicalAccuracy"><span class="title">Magic Accuracy</span>"201"</div>
<div id="magicalCriticalRight"><span class="title">Crit Spell</span>"201"</div>
<div id="castingTimeRatio"><span class="title">Casting Speed</span>"201"</div>
<div id="block"><span class="title">Block</span>"201"</div>
<div id="dodge"><span class="title">Evasion</span>"201"</div>
</div>
接下來,我使用下面的代碼來檢索上述第一個html代碼塊。
HtmlDocument doc = new HtmlDocument();
doc.Load(MyTestFile);
foreach(var node in doc.DocumentNode.SelectNodes("//div[@id='playerStats']/div/span"))
{
Console.WriteLine(node.InnerText + " " + (node.NextSibling != null ? node.NextSibling.InnerText : null));
}
我已經使用的WebRequest,Web客戶端,web瀏覽器和HtmlWeb-agilitypack班拉html文件從網上下來。然而,我希望從中提取的最重要的部分並沒有被拉下來,這是與Hp,mp等有關的值的文檔中。期望值在上面的第二個html代碼塊中描述。
如何讓我的代碼在文檔中記下這個簡單的文本以供我解析?
@Tanner更新答案 – 2013-05-06 02:40:30
謝謝!這很棒! – Tanner 2013-05-06 02:41:33