forked from piratetone/ruby_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkvalidator.rb
More file actions
39 lines (30 loc) · 724 Bytes
/
linkvalidator.rb
File metadata and controls
39 lines (30 loc) · 724 Bytes
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
33
34
35
36
37
38
require 'uri'
require 'open-uri'
require 'rubyful_soup'
begin
print "\n\nEnter website to crawl (ex. http://reddit.com): "
url = gets
puts url
uri = URI.parse(url)
html = open(uri).read
rescue Exception => e
print "Unable to connect to the url: "
puts "Error ---- #{e}"
end
soup = BeautifulSoup.new(html)
links = soup.find_all('a').map { |a| a['href'] }
links.delete_if { |href| href =~ /javascript|mailto/ }
links.each do |l|
if l
begin
link = URI.parse(l)
link.scheme ||= 'http'
link.host ||= uri.host
link.path = uri.path + link.path unless link.path[0] == //
link = URI.parse(link.to_s)
open(link).read
rescue Exception => e
puts "#{link} failed because #{e}"
end
end
end