我想通过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> <strong>Perfect inside and out</strong></li>
<li><span>Mechanical Condition:</span> <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的问题
答案 0 :(得分: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']");