progs/crawler1.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 29 Nov 2018 02:18:05 +0000
changeset 613 bfd511b7ecbf
parent 561 164bcaaedf88
child 722 14914b57e207
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
     1
// A crawler which checks whether there are 
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
     2
// dead links in web-pages
101
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
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
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 101
diff changeset
     8
// gets the first 10K of a web-page
99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
     9
def get_page(url: String) : String = {
428
a47c4227a0c6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 427
diff changeset
    10
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
a47c4227a0c6 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 427
diff changeset
    11
    getOrElse { println(s"  Problem with: $url"); ""}
99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 96
diff changeset
    12
}
561
164bcaaedf88 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    13
get_page("https://nms.kcl.ac.uk/christiana.urban/")
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// regex for URLs
427
546f2090ce12 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 421
diff changeset
    15
val http_pattern = """"https?://[^"]*"""".r /*@\label{httpline}@*/ 
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
101
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    17
// drops the first and last character from a string
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
def unquote(s: String) = s.drop(1).dropRight(1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
254
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
    20
def get_all_URLs(page: String) : Set[String] = 
427
546f2090ce12 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 421
diff changeset
    21
  http_pattern.findAllIn(page).map(unquote).toSet /*@\label{findallline}@*/
254
dcd4688690ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 242
diff changeset
    22
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
    23
// naive version of crawl - searches until a given depth,
1
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 {
95
dbe49327b6c5 added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    28
    println(s"Visiting: $n $url")
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  }
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
242
35104ee14f87 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
    33
// some starting URLs for the crawler
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    34
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 432
diff changeset
    35
//val startURL = """https://nms.kcl.ac.uk/luc.moreau/"""
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents: 3
diff changeset
    36
561
164bcaaedf88 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    37
crawl(startURL, 2)
432
55be90b2a642 added pictures
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 428
diff changeset
    38