links
authorChristian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 26 Sep 2013 10:39:23 +0100
changeset 101 4758a6155878
parent 100 cbc2270c2938
child 102 1ab41c59e3d3
links
progs/crawler1.scala
progs/crawler2.scala
progs/crawler3.scala
--- a/progs/crawler1.scala	Thu Sep 26 10:36:24 2013 +0100
+++ b/progs/crawler1.scala	Thu Sep 26 10:39:23 2013 +0100
@@ -1,8 +1,11 @@
+// A crawler which checks whether there
+// are problems with links in web-pages
+
 import io.Source
 import scala.util.matching.Regex
 import scala.util._
 
-// gets the first ~10K of a page
+// gets the first ~10K of a web-page
 def get_page(url: String) : String = {
   Try(Source.fromURL(url).take(10000).mkString) getOrElse 
     { println(s"  Problem with: $url"); ""}
@@ -11,6 +14,7 @@
 // regex for URLs
 val http_pattern = """\"https?://[^\"]*\"""".r
 
+// drops the first and last character from a string
 def unquote(s: String) = s.drop(1).dropRight(1)
 
 def get_all_URLs(page: String) : Set[String] = {
@@ -32,7 +36,5 @@
 //val startURL = """http://www.inf.kcl.ac.uk/staff/mml/"""
 
 
-// call on the command line 
 crawl(startURL, 2)
 
-crawl("""http://www.inf.kcl.ac.uk/staff/urbanc/bsc-projects-13.html""", 2)
--- a/progs/crawler2.scala	Thu Sep 26 10:36:24 2013 +0100
+++ b/progs/crawler2.scala	Thu Sep 26 10:39:23 2013 +0100
@@ -5,7 +5,7 @@
 import scala.util.matching.Regex
 import scala.util._
 
-// gets the first ~10K of a page
+// gets the first ~10K of a web-page
 def get_page(url: String) : String = {
   Try(Source.fromURL(url).take(10000).mkString) getOrElse 
     { println(s"  Problem with: $url"); ""}
@@ -28,7 +28,7 @@
 // visits pages potentially more than once
 def crawl(url: String, n: Int) : Unit = {
   if (n == 0) ()
-  //else if (my_urls.findFirstIn(url) == None) ()
+  else if (my_urls.findFirstIn(url) == None) ()
   else {
     println(s"Visiting: $n $url")
     for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
--- a/progs/crawler3.scala	Thu Sep 26 10:36:24 2013 +0100
+++ b/progs/crawler3.scala	Thu Sep 26 10:39:23 2013 +0100
@@ -5,7 +5,7 @@
 import scala.util.matching.Regex
 import scala.util._
 
-// gets the first ~10K of a page
+// gets the first ~10K of a web-page
 def get_page(url: String) : String = {
   Try(Source.fromURL(url).take(10000).mkString) getOrElse 
     { println(s"  Problem with: $url"); ""}