繁体   English   中英

使用C#解析HTML的问题

[英]html parsing problem using C#

这里开始 ,我尝试每隔10分钟从股票报价中获取数据。

我使用WebClient下载页面内容并使用正则表达式进行解析。 对于其他网址,它工作正常。 对于特殊URL ,我的解析代码不起作用。

我认为这是javascript的问题,当我在浏览器中加载页面时,加载页面内容后,花了一些额外的时间来绘制数据。 可能是这个人为此页面使用了一些客户端脚本。 谁能帮我...

HTML Agility Pack将为您节省很多麻烦。 尝试使用它,而不是使用正则表达式来解析HTML。

值得一说的是,在链接到报价数据的页面中的确使用Javascript代码,请查看http://www.nseindia.com/js/getquotedata.jshttp://www.nseindia.com/js/quote_data .js

根据@Vinko Vrsalovic的回答, Html Agility pack是您的朋友。 这是一个样本

  WebClient client = new WebClient();
  string source = client.DownloadString(url);

  HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
  document.LoadHtml(source);

  HtmlNodeCollection nodes = document.DocumentNode.SelectNodes("//*[@href]");

   foreach (HtmlNode node in nodes)
   {
    if (node.Attributes.Contains("class"))
    {
     if (node.Attributes["class"].Value.Contains("StockData"))
     {// Here is our info }
    }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM