ruby及watir统计网页中链接数量的语句

有时候需要统计网页中或者网页某个区域中的链接数量,刚开始我尝试以下语句,但不成功:
ie.links(:id, /bra bra/).length

后来发现以下语句可行:
ie.div(:id, “searchList”).links

之后发现如下更有效的语句:

links_searchlist = ie101.links.find_all { |link| link.class_name == ‘permalink’ }
puts “the links in the searchlist:”
puts links_searchlist.length


searchlistlinks = ie102.links.find_all { |link| link.id =~ /hitURL/ }
links_searchlist = searchlistlinks.length
puts links_searchlist

searchlistlinks = ie33.links.find_all { |link| link.href =~ /something/ }
类似的语句不行:searchlistlinks = ie33.links.find_all { |link| link.url =~ /something/ }

最近发现,如果链接数量比较多,以上语句耗时太长,通过google和百度,找到了更高效的语句:

links = ie51.div(:id, ‘threadlist’).html.scan(/something/).count
puts links

我自己的原创: http://www.sdgz.cn

你可能感兴趣的:(html,Google,百度,IE,Ruby)