Ich bin gerade dabei, XML-Dokumente zu durchlaufen, um zu lernen, wie man xpath benutzt.
Ich bin auf ein Problem gestoßen. Jedes Mal, wenn ich versuche, meinen XPath auszuführen, gibt es null
als ob es nichts finden würde. Ich habe den Xpath in XMLQuire ausprobiert und es hat dort funktioniert.
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)
{
}
}
}
Ich habe den anderen xpath, den ich ausprobiert habe, auskommentiert. Ich habe diese beiden ausprobiert und beide kehren null
Warum ist das so?
Hier ist ein Bild, das den Teil des DOM zeigt, auf den ich zugreifen möchte.
<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>
Meine Lösung (Ich brauche Hilfe, um meinen XPath in eine Zeile zu bringen, anstatt mit einer Reihe von Schleifen durchzulaufen.)
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();
Ich habe es geschafft, es zu beheben, aber ich bin bis neugierig, warum dieser Xpath funktioniert
//table[contains(@class, 'top-feature')]/tr/td[@class='description']/a
Und dieses tut nicht. //table[contains(@class,'top-feature')]/tbody/tr/td/a
Wie im Kommentar erwähnt, wird das <tbody>
-Element von einem Browser-Entwickler-Tool generiert.
Wenn Sie Ihr var DOM
Objekt zur Laufzeit mit dem Debugger betrachten, können Sie die InnerHtml
Eigenschaft sehen.
<table class="regular-ad js-hover" data-ad-id=".." data-vip-url="..">
<tr>
<td class="watchlist">
...
</td>
<td class="image">
...
</td>
...
</tr>
</table>
Kein <tbody>
-Element, also muss dein XPath so aussehen:
DOM.DocumentNode.SelectNodes("//table[contains(@class,'top-feature')]/tr/td/a");