我正在尝试获取 href
<a>
中的值使用 Nokogiri 的 HTML 标签。
我想确定它们是路径、文件、URL,还是 <div>
编号。
我目前的工作是:
hrefvalue = []
html.css('a').each do |atag|
hrefvalue << atag['href']
end
href
中的可能值可能是:
somefile.html
http://www.someurl.com/somepath/somepath
/some/path/here
#previous
是否有一种机制来识别该值是否是有效的完整 URL、文件、路径或其他?
最佳答案
尝试 URI:
require 'uri'
URI.parse('somefile.html').path
=> "somefile.html"
URI.parse('http://www.someurl.com/somepath/somepath').path
=> "/somepath/somepath"
URI.parse('/some/path/here').path
=> "/some/path/here"
URI.parse('#previous').path
=> ""
关于html - 如何识别 href 中的路径/文件/url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018406/