0

如果有人可以提供帮助,我将不胜感激!我正在尝试解析 Groupon 网站http://www.groupon.com/browse/chicago?category=activities-and-nightlife的以下页面

  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: Object reference not set to an instance of an object in line with content_block

我做错了什么=(提前谢谢!

4

1 回答 1

0

问题似乎是当没有找到节点而不是空集合时,selectnodes 不返回任何内容或返回 null。所以这意味着你可能应该环绕if (content_block != null) {上面的代码块。

于 2012-11-09T21:11:20.140 回答