有没有适用于Ruby的cURL库?
使用OpenURI来操作URL。
open("http://...", :http_basic_authentication=>[user, password])
访问需要HTTP身份验证的网站/页面/资源。
eat
gem是OpenURI的“替代品”,因此您需要首先安装gem eat
$ gem install eat
require 'eat'
eat('http://yahoo.com') #=> String
eat('/home/seamus/foo.txt') #=> String
eat('file:///home/seamus/foo.txt') #=> String
它在底层使用了HTTPClient。 它还具有一些选项:
eat('http://yahoo.com', :timeout => 10) # timeout after 10 seconds
eat('http://yahoo.com', :limit => 1024) # only read the first 1024 chars
eat('https://yahoo.com', :openssl_verify_mode => 'none') # don't bother verifying SSL certificate
curl
命令,那么有一个在线工具可以将其转换为ruby (2.0+)代码:curl-to-ruby。目前,它支持以下选项:-d/--data
、-H/--header
、-I/--head
、-u/--user
、--url
和-X/--request
。它是开放的,欢迎贡献。base = "http://media.pragprog.com/titles/ruby3/code/samples/tutthreads_"
for i in 1..50
url = "#{ base }#{ i }.rb"
file = "tutthreads_#{i}.rb"
File.open(file, 'w') do |f|
system "curl -o #{f.path} #{url}"
end
end