我需要找到两个使用ruby open-uri的网站之间的距离。使用
def check(url)
site = open(url.base_url)
link = %r{^<([a])([^"]+)*([^>]+)*(?:>(.*)<\/\1>|\s+\/>)$}
site.each_line {|line| puts $&,$1,$2,$3,$4 if (line=~link)}
p url.links
end
查找链接无法正常工作。有什么想法吗?
答案 0 :(得分:3)
如果你想找到a
标签&#39; href
参数,使用正确的工具,这通常不是正则表达式。您更有可能使用HTML / XML解析器。
Nokogiri是Ruby的首选解析器:
require 'nokogiri'
require 'open-uri'
doc = Nokogiri.HTML(open('http://www.example.org/index.html'))
doc.search('a').map{ |a| a['href'] }
pp doc.search('a').map{ |a| a['href'] }
# => [
# => "/",
# => "/domains/",
# => "/numbers/",
# => "/protocols/",
# => "/about/",
# => "/go/rfc2606",
# => "/about/",
# => "/about/presentations/",
# => "/about/performance/",
# => "/reports/",
# => "/domains/",
# => "/domains/root/",
# => "/domains/int/",
# => "/domains/arpa/",
# => "/domains/idn-tables/",
# => "/protocols/",
# => "/numbers/",
# => "/abuse/",
# => "http://www.icann.org/",
# => "mailto:iana@iana.org?subject=General%20website%20feedback"
# => ]
答案 1 :(得分:1)
我看到这个正则表达式有几个问题:
空格必须位于空标记中的尾部斜杠之前,但正则表达式需要它
您的正则表达式非常详细且冗余
请尝试以下操作,它会从&lt; a&gt;中提取您的网址。标记:
link = /<a \s # Start of tag
[^>]* # Some whitespace, other attributes, ...
href=" # Start of URL
([^"]*) # The URL, everything up to the closing quote
" # The closing quotes
/x # We stop here, as regular expressions wouldn't be able to
# correctly match nested tags anyway