2014-06-28 4 views
5

Also, wenn ich dieses Stück Code habenSelect-Element von Attributwert mit XPath in Nokogiri

<body> 
    <div class="red"> 
    <a href="http://www.example.com>Example</a> 
    </div> 
</body> 

Ich weiß, dass ich ein Element mit dem Attribut „class“ und Wert „rot“ zu bekommen, aber ich don Ich weiß nicht, wo es sich befindet.

Wenn ich XPath verwendet, ist dieses Stück Code richtig?

dir = "http://www.domain.com" 
doc = Nokogiri::HTML(open(url)) 
doc.xpath('.//*[class="red"]') 

Ich lerne gerade so, ich weiß nicht, ob irgendetwas davon falsch ist. Ich kann es nicht funktionieren lassen. Vielen Dank.

Edit: Nun, es funktioniert =)

doc.xpath('//*[@class="red"]') 
+1

sollte es @klasse sein. Initial * dot * ist in diesem Fall nicht sinnvoll. –

+0

http://stackoverflow.com/questions/2901241/how-to-access-attributes-using-nokogiri – dmitry

Antwort

9

ändern Klasse @class. Entfernen Sie den Punkt am Anfang. Dann wird es funktionieren.