我正在浏览XML文档以了解如何使用xpath。
我偶然发现了一个问题。每当我尝试执行我的xpath时,它都返回null
,好像它没有找到任何东西。
我在XMLQuire中尝试过xpath,它在那里工作。
class Program
{
private static string URL = "https://www.kijiji.ca/b-renovation-contracting-handyman/ontario/home-renovations/k0c753l9004";
private static HtmlWeb client = new HtmlWeb();
static void Main(string[] args)
{
var DOM = client.Load(URL); // //table/tbody/tr/td[@class = 'description']/p
var Featured = DOM.DocumentNode.SelectNodes("//table[contains(@class,'top-feature')]/tbody/tr/td/a");
foreach (var Listing in Featured)
{
}
}
}
我注释掉了我试过的其他xpath,我已经尝试过这两个并且两个都返回null
为什么会这样?
<table class="top-feature js-hover" data-ad-id="1299717863" data-vip-url="/v-renovation-contracting-handyman/sudbury/c-l-contracting-any-job-big-or-small/1299717863">
<tbody><tr>
<td class="watchlist">
<div class="watch js-hover p-vap-lnk-actn-addwtch" data-action="add" data-adid="1299717863" title="Click to add to My Favourites"><div class="icon"></div></div>
<input id="watchlistXsrf" name="ca.kijiji.xsrf.token" value="1527418405414.9b71d1309fdd8a315258ea5a3dac1a09e4a99ec7f32041df88307c46e26a5b1b" type="hidden">
</td>
<td class="image">
<div class="multiple-images"><img src="https://i.ebayimg.com/00/s/NjAwWDgwMA==/z/fXEAAOSwaZdZxTv~/$_2.JPG" alt="C.L. Contracting. Any job big or small."></div>
</td>
<td class="description">
<a href="/v-renovation-contracting-handyman/sudbury/c-l-contracting-any-job-big-or-small/1299717863" class="title ">
C.L. Contracting. Any job big or small.</a>
<p>
Contractor handyman home renovations and repairs. Contractor for Dollarama, Rexall, LaSenza and more. Fully licensed and insured. Able to do drywall, decks, framing, plumbing, flooring windows, ...</p>
<p class="details">
</p>
</td>
<td class="posted">
</td>
</tr>
</tbody></table>
我的解决方案(需要帮助使我的xpath成为1行而不是遍历一堆循环。)
private static string URL = "https://www.kijiji.ca/b-renovation-contracting-handyman/ontario/home-renovations/k0c753l9004";
private static HtmlWeb client = new HtmlWeb();
static void Main(string[] args)
{
var DOM = client.Load(URL); // //table/tbody/tr/td[@class = 'description']/p
var Featured = DOM.DocumentNode.SelectNodes("//table[contains(@class,'top-feature')]/tbody/tr/td/a");
foreach (var table in DOM.DocumentNode.SelectNodes("//table[contains(@class, 'top-feature')]"))
{
Console.WriteLine($"Found: {table}");
foreach (var rows in table.SelectNodes("tr"))
{
Console.WriteLine(rows);
foreach (var cell in rows.SelectNodes("td[@class='description']/a"))
{
Console.WriteLine(cell.InnerText.Trim());
}
}
}
Console.ReadKey();
我已经设法解决了这个问题,但是我很好奇,直到为什么这个xpath工作
//table[contains(@class, 'top-feature')]/tr/td[@class='description']/a
这个没有。
//table[contains(@class,'top-feature')]/tbody/tr/td/a
答案 0 :(得分:0)
正如评论中所提到的,<tbody>
元素是由浏览器开发人员工具生成的。
如果在运行时使用调试器查看var DOM
对象,则可以看到InnerHtml
属性。
<table class="regular-ad js-hover" data-ad-id=".." data-vip-url="..">
<tr>
<td class="watchlist">
...
</td>
<td class="image">
...
</td>
...
</tr>
</table>
没有<tbody>
元素,因此您的XPath必须如下所示:
DOM.DocumentNode.SelectNodes("//table[contains(@class,'top-feature')]/tr/td/a");