Ani*_*inh 2 ruby hpricot nokogiri ruby-on-rails-3
我必须开发一个Ruby on Rails应用程序,它从网页中获取所有图像,pdf,cgi等文件扩展名链接.
从页面获取链接的最简单方法是使用URI.extract.来自文档:
描述
从字符串中提取URI.如果给定块,则遍历所有匹配的URI.如果给定块或具有匹配的数组,则返回nil.
用法
require "uri"
URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
# => ["http://foo.example.com/bla", "mailto:test@example.com"]
Run Code Online (Sandbox Code Playgroud)
看这个页面:
require 'open-uri'
require 'uri'
html = open('http://stackoverflow.com/questions/8722693/how-to-get-all-image-pdf-and-other-files-links-from-a-web-page/8724632#8724632').read
puts URI.extract(html).select{ |l| l[/\.(?:gif|png|jpe?g)\b/]}
Run Code Online (Sandbox Code Playgroud)
返回:
http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png
http://sstatic.net/stackoverflow/img/apple-touch-icon.png
http://foobar.com/path/to/file.gif?some_query=1
http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4157 次 |
| 最近记录: |