2016-12-28 2 views
1

Ich habe HTML-Seite:rasche Verschrottung mit Kanna und XPath

... 
// Product 1 
<div class="lst_main"> 
    <a href="link1.html"> 
    <span> Product 1 name </span> 
    <div class="lst_meta"> 
     <span> Product1 $price</span> 
    </dev> 
</div> 
// Product 2 
<div class="lst_main"> 
</div> 
.... 
// Product N 
<div class="lst_main"> 
</div> 
.... 

Ich brauche URL, Namen und Preis für jedes Produkt zu erhalten. Für diese Aufgabe benutze ich Kanna lib und hier ist mein Code:

Mit diesem Code kann ich jedes Produkt bekommen. Aber ich konnte nicht verstehen, wie man Link, Name und Preis für jedes Produkt bekommt?

Antwort

4

fand ich die Lösung:

for product in doc.xpath("//div[@class='lst_main']") { 
     let productURL = product.at_xpath('a') 
     print(productURL?["href"]) 
     let productName = product.at_xpath('span') 
     print(productName?.text) 
     let productPrice = product.at_xpath('div[@class='lst_meta']/span') 
     print(productPrice?.text) 
}