crawler1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 24 Nov 2012 14:58:27 +0000
changeset 77 49c0beef79a1
parent 64 2d625418c011
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
import io.Source
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import scala.util.matching.Regex
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// gets the first ~10K of a page
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
def get_page(url: String) : String = { 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  try {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
    Source.fromURL(url).take(10000).mkString  
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  catch {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    case e => {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
      println("  Problem with: " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
      ""
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// regex for URLs
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
val http_pattern = """\"https?://[^\"]*\"""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def unquote(s: String) = s.drop(1).dropRight(1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def get_all_URLs(page: String) : Set[String] = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  (http_pattern.findAllIn(page)).map { unquote(_) }.toSet
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
// naive version - seraches until a given depth
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// visits pages potentially more than once
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
def crawl(url: String, n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
    println("Visiting: " + n + " " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
3
Christian Urban <urbanc@in.tum.de>
parents: 1
diff changeset
    37
// staring URL for the crawler
Christian Urban <urbanc@in.tum.de>
parents: 1
diff changeset
    38
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents: 3
diff changeset
    39
//val startURL = """http://www.inf.kcl.ac.uk/staff/mml/"""
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents: 3
diff changeset
    40
3
Christian Urban <urbanc@in.tum.de>
parents: 1
diff changeset
    41
Christian Urban <urbanc@in.tum.de>
parents: 1
diff changeset
    42
// call on the command line 
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
crawl(startURL, 2)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 7
diff changeset
    45
crawl("""http://www.dcs.kcl.ac.uk/staff/urbanc/msc-projects-12.html""", 2)