require 'open-uri'
def downLoadJpg(fileName, uri)
data = open(uri, 'User-Agent' => 'ruby'){|f| f.read}
file = File.new fileName, 'w+'
file.binmode
file << data
file.flush
file.close
end
doc.search('//html/body/card_list/card').each do |card|
puts(card.text)
t = card.attribute("t")
cid = card.attribute("cid")
puts(t)
puts(cid)
fileName = "e:/" + t + ".jpg"
downLoadJpg(fileName, card.text)
end
出现
错误:
引用ruby/1.9.1/open-uri.rb:346:in `open_http': 403 Forbidden (OpenURI::
HTTPError)
def downLoadJpg(fileName, uri)
data = open(URI::encode(uri), "User-Agent" => "Ruby/#{RUBY_VERSION}"){|f| f.read}
file = File.new fileName, 'w+'
file.binmode
file << data
file.flush
file.close
end
主要是我在循环里面不断的去打开同一服务器上的不同的文件,就被禁止掉了