【问题标题】:How to seek a string variable (an html files source code)如何查找字符串变量(一个 html 文件源代码)
【发布时间】:2010-10-02 21:58:02
【问题描述】:

如何寻找这个 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());
 }

}

【问题讨论】:

  • 我不明白你的问题,但你可以尝试使用正则表达式..
  • 当您点击该网站时,您将看到股票汇率。其中之一称为 IMKB。我需要获得 IMKB 的价值。我可以使用上面的代码下载源代码,但我需要在上面的代码中的这个 loong sb 字符串中找到这个值,你可能会看到..

标签: c# .net asp.net seek


【解决方案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 元素。

【讨论】:

    【解决方案2】:
        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,它将失败

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2015-06-17
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多