Getting attribute’s value in Nokogiri to extract link URLs

html = <<HTML <div id=”block”> <a href=”http://google.com”>link</a> </div> HTML doc = Nokogiri::HTML(html) doc.xpath(‘//div/a/@href’) #=> [#<Nokogiri::XML::Attr:0x80887798 name=”href” value=”http://google.com”>] Or if you wanna be more specific about the div: >> doc.xpath(‘//div[@id=”block”]/a/@href’) => [#<Nokogiri::XML::Attr:0x80887798 name=”href” value=”http://google.com”>] >> doc.xpath(‘//div[@id=”block”]/a/@href’).first.value => “http://google.com”

Nokogiri/Xpath namespace query

All namespaces need to be registered when parsing. Nokogiri automatically registers namespaces on the root node. Any namespaces that are not on the root node you have to register yourself. This should work: puts doc.xpath(‘//dc:title’, ‘dc’ => “URI”) Alternately, you can remove namespaces altogether. Only do this if you are certain there will be no … Read more

extract links (URLs), with nokogiri in ruby, from a href html tags?

You can do it like this: doc = Nokogiri::HTML.parse(<<-HTML_END) <div class=”heat”> <a href=”http://example.org/site/1/”>site 1</a> <a href=”http://example.org/site/2/”>site 2</a> <a href=”http://example.org/site/3/”>site 3</a> </div> <div class=”wave”> <a href=”http://example.org/site/4/”>site 4</a> <a href=”http://example.org/site/5/”>site 5</a> <a href=”http://example.org/site/6/”>site 6</a> </div> HTML_END l = doc.css(‘div.heat a’).map { |link| link[‘href’] } This solution finds all anchor elements using a css selector and collects their … Read more

Error while installing Nokogiri (1.6.7) on El Capitan

You should install xcode-select packages first, then try installing nokogiri again. Try these commands, xcode-select –install then try gem install nokogiri with whatever Nokogiri version you want. Nokogiri depends on multiple libraries like libxslt, libxml and zlib. Dev versions (including source) of these should be installed before installing Nokogiri in any Linux distribution. For OS … Read more

Installing Nokogiri on OSX 10.10 Yosemite

I managed to install Nokogiri under Yosemite (OS X 10.10 Preview). Step 1: Install Brew Skip this if brew was installed. ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” Step 2: Install brew libs brew tap homebrew/dupes brew install libxml2 libxslt brew install libiconv Step 3: Download and install Apple Commandline Tools for 10.10 It’s important that you … Read more

Error installing nokogiri: Failed to build gem native extension & libiconv is missing (OSX)

Run these commands: gem uninstall nokogiri xcode-select –install gem install nokogiri source: http://www.nokogiri.org/tutorials/installing_nokogiri.html#mac_os_x sometimes mac updates can break xcode CLI so reinstalling can fix the issue: https://github.com/sparklemotion/nokogiri/issues/1445

How to access attributes using Nokogiri

Using Nokogiri::XML::Reader works for your example, but probably isn’t the full answer you are looking for (Note that there is no attributes method for Builder). reader = Nokogiri::XML::Reader(builder.to_xml) reader.read #Moves to next node in document reader.attribute(“messageId”) Note that if you issued reader.read again and then tried reader.attribute(“messageId”) the result will be nil since the current … Read more