2012-11-09 51 views
0

如果有人能帮忙,我将不胜感激!我试图解析团购网站的下页http://www.groupon.com/browse/chicago?category=activities-and-nightlife如何使用HtmlAgilityPack解析html的多个节点?

var webGet = new HtmlWeb(); 
    var deal1 = webGet.Load("http://www.groupon.com/browse/chicago?category=activities-and-nightlife"); 

我想要得到每笔交易的整个块(即用于折扣优惠)

HtmlNodeCollection content_block = deal1.DocumentNode.SelectNodes("//div[@class = 'deal-list-tile grid_5_third']"); 

接着出来,我想每个块的以获得标题,公司名称,位置和价格。

foreach(HtmlNode node in content_block) 
     { 
      string title2 = node.SelectSingleNode("//div[@class = 'deal-title js-permalink']").InnerText; 
      string country2 = node.SelectSingleNode("//p[@class = 'merchant-name']").InnerText; 
      string location2 = node.SelectSingleNode("//p[@class = 'location']").InnerText; 
      string price2 = node.SelectSingleNode("//div[@class = 'price']/span").InnerText; 
     } 

在这里,我感到困惑,我需要写所有关于交易的信息到 DbSet<Deal> Deals,但即使我尝试内容显示为ViewBag.Message = title + country + location + price;我得到System.NullReferenceException:对象引用未设置为实例与content_block一致的对象。

什么我提前做错=( 谢谢!如果没有节点被发现的,而不是一个空的集合

+0

在你列出的页面中,我甚至找不到具有该名称的类[当我查看页面源代码时],你可能有更好的运气访问他们的API https://sites.google .com/site/grouponapiv2/home – marvin

回答

0

这个问题似乎是在的selectNodes返回任何内容或空。所以这意味着你应该换if (content_block != null) {围绕你的代码块上面

+0

是的,但它不显示任何东西,HtmlNodeCollection似乎是空的,但我选择的div节点 –