这是一个完整的解决方案:
require 'open-uri'
if open('http://example.com/').read =~ /searchword/
# do something
end
Run Code Online (Sandbox Code Playgroud)
对于像这样简单的东西,我宁愿写几行代码而不是使用完整的宝石.这是我要做的:
require 'net/http'
# let's take the url of this page
uri = 'http://stackoverflow.com/questions/1878891/how-to-load-a-web-page-and-search-for-a-word-in-ruby'
response = Net::HTTP.get_response(URI.parse(uri)) # => #<Net::HTTPOK 200 OK readbody=true>
# match the word Ruby
/Ruby/.match(response.body) # => #<MatchData "Ruby">
Run Code Online (Sandbox Code Playgroud)
如果我需要做更多的事情,我可以去使用宝石的路径,我需要实现一些已经在其中一个宝石中完成的算法