How to seek a string variable (an html files source code)

落花浮王杯 提交于 2019-12-12 06:52:06

问题


How can I seek this sb string variable to get those variables:IMKB's value: 64882,72 how can I get it please show the seek idea

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());
 }

}

回答1:


Looking at the source of the page in question, it looks like the value you want is in this block of 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>

While it says that it is XHTML, it doesn't appear to be valid XML, so parsing it via XPath isn't a solution.

I would see if the id of "ctl00_ContentPlaceHolderIndex_lblFVeriIMKBP" is always used, and use regular expression to find the span element who's attribute "id" is that value.




回答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();

but i don't think that's a good idea. it will fail if they change they element's ID



来源:https://stackoverflow.com/questions/3847677/how-to-seek-a-string-variable-an-html-files-source-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!