ruby - How do I parse this HTML using Nokogiri? -
based on html:
<li><strong><a href="http://www.ukasta.org.uk/">united kingdom agricultural supply trade association</a> (ukasta)</strong></li>
i want united kingdom agricultural supply tradeassociation
, (ukasta)
strings.
using nokogiri, wrote:
linklist=link.parent.parent.css('li strong a') linklist.each |f| puts f.text end
f.text
"united kingdom agricultural supply tradeassociation", how "(ukasta)"?
you're diving in deep. i'd use:
require 'nokogiri' html = '<li><strong><a href="http://www.ukasta.org.uk/">united kingdom agricultural supply trade association</a> (ukasta)</strong></li>' doc = nokogiri::html(html) doc.at('strong').text
which returns:
"united kingdom agricultural supply trade association (ukasta)"
if have find <a>
node, can access "(ukasta)" using:
a_node = doc.at('a') a_node.text => "united kingdom agricultural supply trade association" a_node.next_sibling.text => " (ukasta)"