progs/crawler2.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 26 Sep 2013 11:05:54 +0100
changeset 104 ffde837b1db1
parent 101 4758a6155878
child 112 95ee5cc5c05d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
100
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     1
// This version of the crawler only
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     2
// checks links in the "domain" urbanc
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     3
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import io.Source
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.util.matching.Regex
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
     6
import scala.util._
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
101
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
     8
// gets the first ~10K of a web-page
100
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     9
def get_page(url: String) : String = {
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
    10
  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
    11
    { println(s"  Problem with: $url"); ""}
100
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
    12
}
1
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
// staring URL for the crawler
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
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
// regex for URLs
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
val http_pattern = """\"https?://[^\"]*\"""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
val my_urls = """urbanc""".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] = {
96
9fcd3de53c06 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 95
diff changeset
    24
  http_pattern.findAllIn(page).map(unquote).toSet
1
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) ()
101
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 100
diff changeset
    31
  else if (my_urls.findFirstIn(url) == None) ()
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  else {
95
dbe49327b6c5 added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    33
    println(s"Visiting: $n $url")
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
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
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
// can now deal with depth 3
3
Christian Urban <urbanc@in.tum.de>
parents: 1
diff changeset
    39
// start on command line
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents: 3
diff changeset
    40
crawl(startURL, 4)
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
100
cbc2270c2938 updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
    42
crawl("""http://www.inf.kcl.ac.uk/staff/urbanc/bsc-projects-13.html""", 2)