5

提供されたhtml(スパン)の間のデータを取得しようとしています(この場合は31)

元のコードは次のとおりです(クロムの要素の検査から)

<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 番号は、誰がサブスクライブするかによって異なります。

4

3 に答える 3

11

これを行うには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>

正規表現は実行可能なオプションですが、htmlを解析するために可能な限り避けたいものです(ここを参照)

持続可能性の観点から、ページソースを理解していることを確認する必要があります(つまり、ページを数回更新し、更新のたびにターゲットスパンが同じ親内にネストされているかどうかを確認し、ページが同じにあることを確認します一般的な形式など...次に、上記の原則を使用してスパンに移動します)。

于 2012-06-25T16:39:58.130 に答える
11

あなたはそれについて信じられないほど具体的である可能性があります:

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;
于 2012-06-25T16:22:48.397 に答える
1

複数の可能性があります。

  1. 正規表現
  2. HTML を XML として解析し、XPath経由で値を取得します。
  3. すべての要素を反復処理します。span タグを使用する場合は、終了の '>' が見つかるまですべての文字をスキップします。次に、必要な値は、次の開始点「<」の前のすべてです

System.Windows.Forms.HtmlDocumentも見てください

于 2012-06-25T16:20:24.873 に答える