How to get the HTML source of a webpage in Ruby

Use Net::HTTP:

require 'net/http'

source = Net::HTTP.get('stackoverflow.com', '/index.html')

require 'open-uri'
source = open(url){|f|f.read}

UPD: more modern syntax

require 'open-uri'
source = open(url, &:read)

require 'open-uri'
source = open(url).read

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页