代码之家  ›  专栏  ›  技术社区  ›  shamim

如何获取c变量中的URL信息?

  •  0
  • shamim  · 技术社区  · 14 年前
    http://www.dsebd.org/latest_PE.php
    

    以上 网址 包含几个信息。从这个网址我只想得到下面的信息。 如何?

    Price Earning Ratio : at a glance
    on Aug 2, 2010 at 11:28:00
    

    我想知道怎么得到 网址 在C中的变量或某个存储容器中的信息。具体来说,我需要以上信息,我不需要其他信息。 我使用以下语法:

    WebClient objWebClient = new WebClient();
    
    aRequestHTML = objWebClient.DownloadData("http://www.dsebd.org/latest_PE.php");//http://www.dsebd.org/latest_PE_all2_08.php
    UTF8Encoding utf8 = new UTF8Encoding();
    myString = utf8.GetString(aRequestHTML);
    HtmlDocument htmlDoc = new HtmlDocument();
    htmlDoc.LoadHtml(myString);
    

    那之后怎么办?我想没什么,请给我讲讲语法

      foreach (HtmlNode node in doc.DocumentNode.SelectNodes(@"//td"))//td[text()="Price Earning Ratio : at a glance"
                {
                    if (node.InnerHtml.Contains("Price Earning Ratio"))
                    {
                        //I get the td value
    
                    }
                    //[text()=Price Earning Ratio : at a glance
                    // do stuff with node   //@"//td[text()=Price Earning Ratio : at a glance"
                }
    

    在添加上述语法之后,我得到下面的输出

    <br>
          <font color="#FFFFFF" size="3" face="Arial"><b>Price Earning Ratio : at 
          a glance <b> </b></b></font><br>
        <font color="#FFFFFF" size="2" face="Arial"><b> on Aug 2, 2010  at 17:04:00<b></b></b></font>
        <br>
    

    从这里我只想得到约会的部分。怎么做?没有这个日期,我不需要任何东西。每天这个日期都会改变,它是动态的日期,所以我不能将这个日期嵌入。 从上面说出我是如何得到约会的。

    2 回复  |  直到 7 年前
        1
  •  4
  •   carla Sergey Berezovskiy    7 年前

    看看 HTML Agility Pack -它是一个HTML解析器,允许您传入一个将解析的URL,以便您可以使用xpath查询它。

    HtmlWeb hw = new HtmlWeb();
    HtmlDocument doc = hw.Load("http://www.dsebd.org/latest_PE.php");
    

    在这一点上 doc 可以查询和查看:

    foreach(HtmlNode node in doc.DocumentNode.SelectNodes("XPATH for interesting nodes"))
    {
      // do stuff with node
    }
    
        2
  •  0
  •   PanJanek    14 年前

    如果您想从上面的URL下载内容,可以使用WebClient(标准的C类来发出HTTP请求)进行HTTP调用,然后 Html Agility Pack 解析HTML并提取数据。