-1

これらの変数を取得するために、この sb 文字列変数をシークするにはどうすればよいですか: IMKB の値: 64882,72 どうすれば取得できますか シークのアイデアを示してください

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;

public partial class Run2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


  // used to build entire input
  StringBuilder sb  = new StringBuilder();

  // used on each read operation
  byte[]        buf = new byte[8192];

  // prepare the web page we will be asking for
  HttpWebRequest  request  = (HttpWebRequest)
   WebRequest.Create("http://www.yapikredi.com.tr/tr-TR/yatirimci_kosesi/main.aspx");


        //http://www.imkb.gov.tr/Home.aspx

  // execute the request
  HttpWebResponse response = (HttpWebResponse)
   request.GetResponse();

  // we will read data via the response stream
  Stream resStream = response.GetResponseStream();

  string tempString = null;
  int    count      = 0;

  do
  {
   // fill the buffer with data
   count = resStream.Read(buf, 0, buf.Length);

   // make sure we read some data
   if (count != 0)
   {
    // translate from bytes to ASCII text
    tempString = Encoding.ASCII.GetString(buf, 0, count);

    // continue building the string
    sb.Append(tempString);
   }
  }
  while (count > 0); // any more data to read?

  // print out page source
  //Console.WriteLine(sb.ToString());
        Response.Write(sb.ToString());
 }

}
4

2 に答える 2

1

問題のページのソースを見ると、必要な値は次の HTML ブロックにあるようです。

<tr> 
    <td width="74" style="padding-left:5px;">IMKB</td> 
    <td width="45" align="right" style="padding-right:3px;"><span id="ctl00_ContentPlaceHolderIndex_lblFVeriIMKBP">64882,72</span></td> 
    <td width="44" align="right" style="padding-right:3px;"><span id="ctl00_ContentPlaceHolderIndex_lblFVeriIMKBD">% -1,35</span></td> 
</tr>

XHTML であると表示されていますが、有効な XML ではないようです。そのため、XPath を介して解析することは解決策ではありません。

「ctl00_ContentPlaceHolderIndex_lblFVeriIMKBP」のIDが常に使用されているかどうかを確認し、正規表現を使用して、属性「id」がその値であるスパン要素を見つけます。

于 2010-10-02T22:18:37.447 に答える
1
    string s = sb.ToString();
    Regex r = new Regex(@"<span id=""ctl00_ContentPlaceHolderIndex_lblFVeriIMKBP"">(.*?)</span>");
    Match m = r.Match(s);
    string value = m.Groups[1].ToString();

しかし、それは良い考えだとは思いません。要素のIDを変更すると失敗します

于 2010-10-02T23:20:54.170 に答える