0

我想通过 HtmlAgilityPack 在 C# 中获取详细信息、描述和发布日期。

<div id="listing-details">
<div id="listing-details-list">
<h3 class="listing-details-header">
Details: <span>Posted on: 2nd July 2013</span>
</h3>
<ul class="important-fields">
    <li class=""><span>Make:</span> <strong>Jeep</strong></li>
    <li class=""><span>Model:</span> <strong>Wrangler</strong></li>
</ul>
<ul>
    <li><span>Body Condition:</span>&nbsp;<strong>Perfect inside and out</strong></li>
    <li><span>Mechanical Condition:</span>&nbsp;<strong>No history </strong></li>
</ul>

    </div>

                <div class="" id="description-text">
                    <h3 class="listing-details-header">
                        Description: 
                    </h3>



                            <div id="dont_gtrans"></div>


                        <div class="trans_toggle_box">
                            <div class="trans_toggle_text" id="trans_toggle_text">
                                <span style="direction: ltr" class="title">

                                        <p>here is description, ;</p>

                                </span>
                            </div>
                        </div>

                </div>

            </div>

这是我的代码

 var rootNode = htmlDoc.DocumentNode;

        var dataNode = rootNode.SelectNodes(".//div[@class='listing-details']");
        string a = "";
        foreach (HtmlNode list in dataNode)
        {
            var dataNode1 = list.SelectSingleNode(".//div[@class='listing-details-list']");
            var carNameNode = dataNode1.SelectSingleNode(".//li/span");
            a = a + carNameNode.InnerText;
        }

这给了我在 foreach 和 foreach 循环内的对象引用 null 的问题

4

1 回答 1

1

您正在查看错误的属性。您要查找的 div 没有 class = 'listing-details' 或 'listing-details-list'。这些是 div 的 ID。

更改您的代码以检查 id 属性:

var dataNode = rootNode.SelectNodes(".//div[@id='listing-details']");

var dataNode1 = list.SelectSingleNode(".//div[@id='listing-details-list']");
于 2013-07-02T08:37:16.183 回答