Skip to content

Instantly share code, notes, and snippets.

@ezefranca
Forked from mussatto/nokogiri.rb
Created January 28, 2022 21:29
Show Gist options
  • Save ezefranca/2fd80a576179e6fde759ea5870a56ea3 to your computer and use it in GitHub Desktop.
Save ezefranca/2fd80a576179e6fde759ea5870a56ea3 to your computer and use it in GitHub Desktop.
testing nokogiri
require 'nokogiri'
html = '
<html>
<body>
<p>foo banana 1</p>
this text
<p>bar</p>
<p>foo banana 2</p>
<p>foo banana 3</p>
<p> else </p>
<p> foo </p>
<p> foo </p>
<p> foo </p>
</body>
</html>
'
keywords = ["else", "banana", "nomatch"]
doc = Nokogiri::HTML(html)
total_matches = 0
keywords.each do |keyword|
p "searching for:"+keyword
elements = doc.search('p:contains("'+keyword+'")')
if(elements)
elements.each do |element|
total_matches=total_matches+1
p element.text
end
else
p "NO MEANS NO!"
end
p "============"
end
p "total matches for keywords:" + total_matches.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment