2012-06-25 53 views
2

我試圖讓提供的HTML(跨度)之間的數據(在這種情況下,31)2個html標籤之間獲取文本C#

這裏是原來的代碼(從檢查中鉻元素)

<span id="point_total" class="tooltip" oldtitle="Note: If the number is black, your points are actually a little bit negative. Don't worry, this just means you need to start subbing again." aria-describedby="ui-tooltip-0">31</span> 

我有豐富的文本框,其中包含頁面的源代碼,這裏是相同的代碼,但在富文本框的第51行:

<DIV id=point_display>You have<BR><SPAN id=point_total class=tooltip jQuery16207621750175125325="23" oldtitle="Note: If the number is black, your points are actually a little bit negative. Don't worry, this just means you need to start subbing again.">17</SPAN><BR>Points </DIV><IMG style="FLOAT: right" title="Gain subscribers" border=0 alt="When people subscribe to you, you lose a point" src="http://static.subxcess.com/images/page/decoration/remove-1-point.png"> </DIV> 

我該怎麼做呢?我嘗試了幾種方法,但他們都沒有爲我工作。

我正在嘗試從此頁檢索分值:http://www.subxcess.com/sub4sub.php 該數字會根據您的潛在客戶而變化。

+0

如果您需要在代碼隱藏中訪問它,您可以向您的範圍添加「runat = server」並獲取內部文本。 – Tim

+0

是一個jQuery解決方案好嗎? –

+0

var yourdata = $('span')。html(); –

回答

7

你可能是令人難以置信的具體看:

var regex = new Regex(@"<span id=""point_total"" class=""tooltip"" oldtitle="".*?"" aria-describedby=""ui-tooltip-0"">(.*?)</span>"); 

var match = regex.Match(@"<span id=""point_total"" class=""tooltip"" oldtitle=""Note: If the number is black, your points are actually a little bit negative. Don't worry, this just means you need to start subbing again."" aria-describedby=""ui-tooltip-0"">31</span>"); 

var result = match.Groups[1].Value; 
+0

這對我來說不是一件事,我應該在原始文章中提到過,值的變化並不總是等於31 –

+0

Got它工作在最後,謝謝:) –

1

有多種可能性。

  1. Regex
  2. 讓HTML解析XML和通過的所有元素獲得通過XPath
  3. 迭代值。如果您使用span標籤,則跳過所有字符,直到找到結尾'>'。然後你需要的值是一切下一開幕前「<」

也期待在System.Windows.Forms.HtmlDocument

8

你要使用HtmlAgilityPack要做到這一點,這很簡單:

HtmlDocument doc = new HtmlDocument; 
doc.Load("filepath"); 

HtmlNode node = doc.DocumentNode.SelectSingleNode("//span"); //Here, you can also do something like (".//span[@id='point_total' class='tooltip' jQuery16207621750175125325='23' oldtitle='Note: If the number is black, your points are actually a little bit negative. Don't worry, this just means you need to start subbing again.']"); to select specific spans, etc... 

string value = node.InnerText; //this string will contain the value of span, i.e. <span>***value***</span> 

正則表達式,而一個可行的o ption,這是你通常想要避免的,如果在所有可能的情況下解析HTML(請參閱Here

就可持續性而言,您需要確保您瞭解頁面源代碼(例如,刷新它並查看每次刷新後您的目標跨度是否嵌套在同一家長中,確保頁面採用相同的一般格式,等等,然後使用上述原則導航到跨度)。

+0

此代碼爲我工作,除了它不斷顯示相同的數字,無論實際值是什麼 –

+0

您是否確保重新加載頁面源? – gfppaste

+0

是的,我有一個定時器設置爲每5秒刷新一次源 –

相關問題