-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”为该值的 span 元素。

于 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 回答