2017-06-22 70 views
0

正如試圖抓住每個標籤發生的每個InnerText並將其添加到列表中的標題所簡述的那樣。這裏是我的代碼爲藏漢我的html:c#HtmlAgilityPack,如何抓住特定標籤的所有出現的InnerText?

HTML的身體:

<body cz-shortcut-listen="true"> 
{"draw":1,"recordsTotal":9437,"recordsFiltered":9437,"data":[[" 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115739257\&quot;">AK-47 | Aquamarine Revenge (Factory New)&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;"href="\&quot;\/id\/115739257\&quot;">33.87&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115739257\&quot;">34.53&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;https:\/\/track.steamanalyst.com\/730\/115739257\/all\&quot;">25.9&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115739257\&quot;">164&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115739257\&quot;">-0.16&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115739257\&quot;">2.10945&lt;\/a&gt;"],["</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115734122\&quot;">AK-47 | Aquamarine Revenge (Minimal Wear)&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115734122\&quot;">23.44&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115734122\&quot;">21.85&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;https:\/\/track.steamanalyst.com\/730\/115734122\/all\&quot;">17.61&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115734122\&quot;">533&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115734122\&quot;">-2.65&lt;\/a&gt;","</a> 
<a target="\&quot;_blank\&quot;" href="\&quot;\/id\/115734122\&quot;">0.94387&lt;\/a&gt;"],["</a> 
</body> 

我的代碼:

List<string> Data = new List<string>(); 
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a[@target]")) 
{ 
    if(j <= 6) 
    { 
     Data.Add(node.InnerText); 
     if (j == 6) 
     { 
      JsonDB.Add(Data[0], Data[1]); 
      Data.Clear(); 
      j = 0; 
     } 
     else 
     { 
      j++; 
     } 
    } 
} 

問題與此代碼:node.InnerText顯示所有InnerTexts的接串身體中的所有標籤!基本上,它顯示了這個在doc.DocumentNode.SelectNodes("//a[@target]")的第一個節點:

AK-47 | Aquamarine Revenge (Factory New)","33.8","34.34","25.89","170", 
"-1.27","2.03181"],[... 

回答

0

身體所有標籤:

doc.DocumentNode.SelectNodes("//a[@target]")) 

標籤在doc:

doc.DocumentNode.SelectNodes(".//a[@target]")) 
+0

@Pang這真的不是問題,但更重要的是,它顯示了所有連接字符串作爲一個節點。基本如此:AK-47 |海藍寶石復仇(新工廠版)「,」33.8「,」34.34「,」25.89「.... – Nikolas

0

解決方案:它有一個像被處理進入HTML之前的JSON對象

JObject jresponse = JObject.Parse(response); 
foreach (JArray row in jresponse["data"]) 
{ 
    List<string> Data = new List<string>(); 
    foreach (JToken entry in row) 
    { 
     doc.LoadHtml(entry.ToString()); 
     HtmlNode node = doc.DocumentNode.SelectSingleNode("//a[@target]"); 
     Data.Add(node.InnerText); 
    } 
}