progs/crawler.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 27 Nov 2013 08:49:51 +0000
changeset 207 f824e1331fc6
parent 93 4794759139ea
permissions -rw-r--r--
added slides
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
// non-existing page -> returns the empty string
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
get_page("""http://www.foobar.com""")
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
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
// staring URL for the crawler
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// starts with an "
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
// then either http or https
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// then ://
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
// then any character that is not "
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// finally "
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
val http_pattern = """\"((?:http|https)://(?:[^\"])*)\"""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
val http_pattern = """\"(https?://[^\"]*)\"""".r
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
def unquote(s: String) = s.drop(1).dropRight(1)
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
def get_all_URLs(page: String) : Set[String] = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  (http_pattern.findAllIn(page)).map { unquote(_) }.toSet
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
// get all urls in startURL
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
get_all_URLs(get_page(startURL))
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// number of all urls in startURL 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
get_all_URLs(get_page(startURL)).toList.length
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// naive version - seraches until a given depth
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// visits pages potentially more than once
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
def crawl(url: String, n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    println("Visiting: " + n + " " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
crawl(startURL, 2)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//breadth-first version without visiting 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
//pages twice
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
def bf_crawl(todo: Set[String], visited: Set[String], n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
    val new_todo = todo.flatMap { 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
      url => {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
        if (visited.contains(url)) Set[String]()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
        else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
          println("Visiting: " + n + " " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
          get_all_URLs(get_page(url))
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
        }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
      }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
    } 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
    bf_crawl(new_todo, visited union todo, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
bf_crawl(Set(startURL1), Set(), 2)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
//breadth-first version without visiting 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
//pages twice and only in "my" domain
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
val my_pattern = """urbanc""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
// breadth first search avoiding double searches
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
def bf_crawl2(todo: Set[String], visited: Set[String], n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
    val new_todo = todo.flatMap { 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
      url => {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
        if (visited.contains(url)) Set[String]()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
        else if (my_pattern.findFirstIn(url) == None) Set[String]()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
        else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
          println("Visiting: " + n + " " + url);
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
          get_all_URLs(get_page(url))
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
        }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
      }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
    } 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
    bf_crawl2(new_todo, visited union todo, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
bf_crawl2(Set(startURL1), Set(), 5)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
// email harvester
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
// from 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
// http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
def bf_crawl3(todo: Set[String], visited: Set[String], n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
    val new_todo = todo.flatMap { 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
      url => {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
        if (visited.contains(url)) Set[String]()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
        else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
          println("Visiting: " + n + " " + url);
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
          val page = get_page(url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
          println(email_pattern.findAllIn(page).mkString("\n"))
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
          get_all_URLs(get_page(url))
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
        }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
      }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
    } 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
    bf_crawl3(new_todo, visited union todo, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
bf_crawl3(Set(startURL1), Set(), 3)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
// depth-first version does not work,
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
// because it might visit pages at depth 1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
// while it still wants to visit them at 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
// depth 2 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
var visited = Set("")
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
def crawl(url: String, n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
  else if (visited.contains(url)) () //println("Already visited: " + n + " " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
    println("Visiting: " + n + " " + url);
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
    visited += url
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
    for (u <- getAllURLs(getURLpage(url))) crawl(u, n - 1);
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
}