Ruby Web image capturing
This article shares with you the code for capturing Web Images Written in ruby. It is very simple and practical. If you need it, you can refer to it.
Some time ago, I saw a lot of scripts written by my sister, and I wrote one myself.
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 |
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
####### Download an image
Def download_img (img_url)
Begin
Img_file = open (img_url) {| f. read}
File_name = img_url.split ('/'). last
# Puts file_name
Open ("public/meizi/" + file_name, "wb") {| f. write (img_file )}
Return "/public/meizi/" + file_name
Rescue => err
Puts err
Return''
End
End
End
|
The above is all the content of this article. I hope you will like it.