progs/crawler2.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 24 Sep 2013 01:56:53 +0100
changeset 96 9fcd3de53c06
parent 95 dbe49327b6c5
child 100 cbc2270c2938
permissions -rw-r--r--
updated
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
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
     3
import scala.util._
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
// gets the first ~10K of a page
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
     6
def get_page(url: String) : String = 
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
     7
  Try(Source.fromURL(url).take(10000).mkString) getOrElse 
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
     8
    { println(s"  Problem with: $url"); ""}
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// staring URL for the crawler
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
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
// regex for URLs
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
val http_pattern = """\"https?://[^\"]*\"""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
val my_urls = """urbanc""".r
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
def unquote(s: String) = s.drop(1).dropRight(1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def get_all_URLs(page: String) : Set[String] = {
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
    20
  http_pattern.findAllIn(page).map(unquote).toSet
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
}
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
// naive version - seraches until a given depth
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// visits pages potentially more than once
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def crawl(url: String, n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  else if (my_urls.findFirstIn(url) == None) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  else {
95
dbe49327b6c5 added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    29
    println(s"Visiting: $n $url")
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// can now deal with depth 3
3
Christian Urban <urbanc@in.tum.de>
parents: 1
diff changeset
    35
// start on command line
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents: 3
diff changeset
    36
crawl(startURL, 4)
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37