sipder获取p站spotlight栏目的图片

require 'httpclient'
require 'hpricot'
require 'mechanize'

@client = Mechanize.new
def get_list
    for i in 2..4
        link = "http://spotlight.pics/zh/?p=#{i}"
        heads={
            "Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding"=>"gzip, deflate, sdch",
        "Accept-Language"=>"zh-CN,zh;q=0.8",
        "Cache-Control"=>"max-age=0",
        "Connection"=>"keep-alive",
        "Cookie"=>"p_ab_id=3; _gat=1; user_lang=zh; _ga=GA1.2.2112927109.1453364740",
        "Host"=>"spotlight.pics",
        "Upgrade-Insecure-Requests"=>"1",
        "User-Agent"=>"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
        }
        params={"value"=>"zh"}
        doc = Hpricot(@client.get(link,params,nil,heads).body)
        doc.search("article[#@class=illust-article]").search("h2").search("a").each do |x|
            puts href = x["href"]
            puts file_name = x.inner_html
            get_one(href,file_name)
        end
    end
end

def get_one(url,file_name)
    doc = Hpricot(@client.get(url).body)
    doc.search("div[@class=illust-wrap]").search("img").each do |x|
        puts src = x["src"]
        puts jpg = src.split("/").last
        s = @client.get(src)
        file_name = file_name[0..15] if file_name.size>20
        if !File.exists?("/home/wping/图片/Wallpapers/P站/#{file_name}/#{jpg}")
            if !Dir.exists?("/home/wping/图片/Wallpapers/P站/#{file_name}")
                Dir.mkdir("/home/wping/图片/Wallpapers/P站/#{file_name}")
                File.open("/home/wping/图片/Wallpapers/P站/#{file_name}/#{jpg}","w+") do |file|
                    puts "save ---------------------------"
                    file.puts s.body
                end
            else
                File.open("/home/wping/图片/Wallpapers/P站/#{file_name}/#{jpg}","w+") do |file|
                    puts "save ---------------------------"
                    file.puts s.body
                end
            end
        end
    end
end

get_list
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值