使用ruby_Hpricot_httpclient 获取网页图片

夏何平
2023-12-01

使用ruby_Hpricot_httpclient 获取百度贴吧的图片

require 'rubygems'    
require 'hpricot'  
require 'open-uri'  
require 'httpclient'

@client = HTTPClient.new
 link = "http://tieba.baidu.com/p/3576130522"
 doc = Hpricot(@client.get(link).body)
 imgs = doc.search("img[@class=BDE_Image]") 获取图片的img标签

for i in 0..imgs.length-1 

    s = @client.get(imgs[i][:src]) 获取img标签中的src
    File.open("[#{i}].jpg","w+") do |file| 将图片保存到文件夹中
        file.puts s.body 
    end
end
 类似资料: