I want to check a line of HTML with a ruby script. Specifically I want to tell the status of local mountain biking trails. When the trails are closed, website displays this image:
When the trails are open, this image:
Code from the website ( usnwc.org ):
<div id="trail-status" class="widget widget_tcstatus">
<div class="tcwstatus">
<div class='trails-current'>
<img src='/wp-content/uploads/2014/10/trailsclosed.png' alt=' Image not found'/>
</div>
</div>
</div>
I could check to see which image is being displayed to return the status. Is there a way to accomplish this in Ruby? Possibly using Net::HTTP
?
You can use open-uri
and regexp
like below:
require 'open-uri'
content = open('http://urltopage.com/trail-path').read
if content =~ /trailsclosed\.png/
puts "Trails closed!"
elsif content =~ /trailsopened\.png/
puts "Trails opened!"
else
puts "Oops, images not found?"
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.