[英]Reading data from HTML source file
在這個網站: http : //eu.battle.net/wow/en/character/Kazzak/Ierina/simple我想得到“560”項目級別的值。
我做了一些研究,並想出了如何獲取所有源代碼
string html = new WebClient().DownloadString(@"http://eu.battle.net/wow/en/character/Kazzak/Ierina/simple");
我認為我應該閱讀的價值在源代碼中:
(<span class="equipped">560</span> Equipped)
或者在這里:
<div id="summary-averageilvl-best" class="best tip" data-id="averageilvl">
560
</div>
我嘗試使用這種方式獲得該值: https : //stackoverflow.com/a/2958449/3935085
我的代碼:
webBrowser1.DocumentText = new WebClient().DownloadString(@"http://eu.battle.net/wow/en/character/Kazzak/Ierina/simple");
HtmlElement ilvl = webBrowser1.Document.GetElementById("equipped");
label1.Text = ilvl.InnerText;
但是,ilvl返回null。
你可以使用正則表達式(正則表達式)。
string input = new WebClient().DownloadString(@"http://eu.battle.net/wow/en/character/Kazzak/Ierina/simple");
// Here we call Regex.Match for <span class="equipped">560</span>
Match match = Regex.Match(input, @"<span class=\""equipped\"">([0-9]+)</span>",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
string key = match.Groups[1].Value; //result here
}
您可以使用HTMLAgilityPack來解析HTML
HtmlDocument html = new HtmlDocument();
html.Load("http://eu.battle.net/wow/en/character/Kazzak/Ierina/simple")
var myValue = html.DocumentNode.SelectNodes("//*[@class=\"equipped\"]");
第一件事:你有一個CLASS "equipped"
的跨度,你試圖獲得一個ID為 "equipped"
的元素
第二件事:您可以嘗試使用正則表達式
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.