【问题标题】:Parsing data from a table from a website with null values从具有空值的网站的表中解析数据
【发布时间】:2013-08-30 17:06:34
【问题描述】:

我对使用 LINQ 有点陌生。我想做的是从一个带有日语词汇的网站上提取数据。表格内有 3 个单元格。有时有些单元格是空白的,因为那里不需要词汇表。 我使用 HTMLAgilityPack 从网站中提取数据。但是,当我尝试解析它时,它会显示一个错误,说它不能有空值。

 HtmlAgilityPack.HtmlDocument doc = hw.Load(@"http://www.tanos.co.uk/jlpt/jlpt1/vocab/combined/");
        var query = from table in doc.DocumentNode.SelectNodes("//table").Cast<HtmlNode>()
                    from row in table.SelectNodes("tr").Cast<HtmlNode>()
                    from cell in table.SelectNodes("th|td").Cast<HtmlNode() //where cell != null
                    select new { Table = table.Id, cellText = cell.InnerText };

我不确定如何转换它,所以我将能够解析我现在拥有的信息。 最终我想使用 foreach 将这些单元格放入一个 excel 文件中。

【问题讨论】:

    标签: c# linq excel html-parsing html-agility-pack


    【解决方案1】:
     var query = from table in doc.DocumentNode.SelectNodes("//table").Cast<HtmlNode>()
                        from row in table.SelectNodes("tr").Cast<HtmlNode>()
                        from cell in table.SelectNodes("th|td").Cast<HtmlNode() //where cell != null
                        select new { Table = table.Id, cellText =cell==null?"":cell.InnerText??"" };
    

    你试过了吗?

    【讨论】:

    • 我确实尝试过类似的方法。问题出在这条语句from cell in table.SelectNodes("th|td").Cast&lt;HtmlNode() 中,因为它在检查它是否为空之前调用了该方法。我只是不确定在 LinQ 中如何解决这个错误。它没有到达select new { Table = table.Id, cellText =cell==null?"":cell.InnerText??"" }; 部分,因为它在它上面的部分失败了。
    猜你喜欢
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2018-05-24
    • 2018-01-14
    • 2018-12-09
    • 2019-02-27
    • 2012-08-25
    • 2012-07-31
    相关资源
    最近更新 更多