Ruby实现网页图片抓取
更新时间:2016年04月10日 16:00:07 作者:yuluo
本文给大家分享的是个人使用ruby编写的抓取网页图片的代码,十分的简单实用,有需要的小伙伴可以参考下。
前段时间看到很多人写的下妹子脚本,自己也写一个
module CommonHelper require 'nokogiri' require 'open-uri' def down_load_xmz site_url = "http://www.xxx.com" for index_page in 1..141 doc_html = Nokogiri::HTML(open(site_url+'/share/comment-page-'+index_page.to_s)) doc_html.css("#comments p img").each do |item_img| puts item_img[:src] download_img(item_img[:src]) end end end ########下载图片 def download_img(img_url) begin img_file = open(img_url) { |f| f.read } file_name = img_url.split('/').last #puts file_name open("public/meizi/"+file_name, "wb") { |f| f.write(img_file) } return "/public/meizi/"+file_name rescue => err puts err return '' end end end
相关文章
使用Ruby on Rails和PostgreSQL自动生成UUID的教程
这篇文章主要介绍了使用Ruby on Rails和PostgreSQL自动生成UUID的教程,主要利用到了PostgreSQL的插件uuid-ossp,需要的朋友可以参考下2015-04-04Ruby on Rails中jquery_ujs组件拖慢速度的问题解决
这篇文章主要介绍了Ruby on Rails中jquery ujs组件拖慢速度的问题解决,jquery_ujs是Rails框架中涉及AJAX交互的组件,需要的朋友可以参考下2015-11-11Ruby中的public、private、protected区别小结
这篇文章主要介绍了Ruby中的public、private、protected区别小结,即Ruby中访问控制符的区别总结,需要的朋友可以参考下2014-08-08
最新评论