progs/crawler.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 05 Sep 2025 16:59:48 +0100
changeset 981 14e5ae1fb541
parent 93 4794759139ea
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
981
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     1
// Try out requests library
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     2
//
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     3
// https://github.com/com-lihaoyi/requests-scala
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     4
//
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     5
//
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     6
import $ivy.`com.lihaoyi::requests:0.9.0`
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     7
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     8
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
     9
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
    10
requests.get("https://nms.kcl.ac.uk/christian.urban/")
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
    11
14e5ae1fb541 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 93
diff changeset
    12
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
import io.Source
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
import scala.util.matching.Regex
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
// gets the first ~10K of a page
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def get_page(url: String) : String = { 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  try {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
    Source.fromURL(url).take(10000).mkString  
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
  catch {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
    case e => {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
      println("  Problem with: " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
      ""
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
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
// non-existing page -> returns the empty string
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
get_page("""http://www.foobar.com""")
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
// staring URL for the crawler
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
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
// starts with an "
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
// then either http or https
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
// then ://
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// then any character that is not "
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
// finally "
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
val http_pattern = """\"((?:http|https)://(?:[^\"])*)\"""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
val http_pattern = """\"(https?://[^\"]*)\"""".r
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
def unquote(s: String) = s.drop(1).dropRight(1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
def get_all_URLs(page: String) : Set[String] = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  (http_pattern.findAllIn(page)).map { unquote(_) }.toSet
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
// get all urls in startURL
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
get_all_URLs(get_page(startURL))
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
// number of all urls in startURL 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
get_all_URLs(get_page(startURL)).toList.length
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
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
// naive version - seraches until a given depth
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
// visits pages potentially more than once
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
def crawl(url: String, n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
    println("Visiting: " + n + " " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
    for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
crawl(startURL, 2)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
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
//breadth-first version without visiting 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//pages twice
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
def bf_crawl(todo: Set[String], visited: Set[String], n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
    val new_todo = todo.flatMap { 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
      url => {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
        if (visited.contains(url)) Set[String]()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
        else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
          println("Visiting: " + n + " " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
          get_all_URLs(get_page(url))
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
        }
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
    } 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
    bf_crawl(new_todo, visited union todo, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
bf_crawl(Set(startURL1), Set(), 2)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
//breadth-first version without visiting 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
//pages twice and only in "my" domain
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
val my_pattern = """urbanc""".r
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
// breadth first search avoiding double searches
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
def bf_crawl2(todo: Set[String], visited: Set[String], n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
    val new_todo = todo.flatMap { 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
      url => {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
        if (visited.contains(url)) Set[String]()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
        else if (my_pattern.findFirstIn(url) == None) Set[String]()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
        else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
          println("Visiting: " + n + " " + url);
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
          get_all_URLs(get_page(url))
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
      }
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
    bf_crawl2(new_todo, visited union todo, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
bf_crawl2(Set(startURL1), Set(), 5)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
// email harvester
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
// from 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
// http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
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
   120
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
def bf_crawl3(todo: Set[String], visited: Set[String], n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
    val new_todo = todo.flatMap { 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
      url => {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
        if (visited.contains(url)) Set[String]()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
        else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
          println("Visiting: " + n + " " + url);
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
          val page = get_page(url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
          println(email_pattern.findAllIn(page).mkString("\n"))
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
          get_all_URLs(get_page(url))
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
        }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
      }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
    } 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
    bf_crawl3(new_todo, visited union todo, n - 1)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
}
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
bf_crawl3(Set(startURL1), Set(), 3)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
// depth-first version does not work,
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
// because it might visit pages at depth 1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
// while it still wants to visit them at 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
// depth 2 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
var visited = Set("")
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
def crawl(url: String, n: Int) : Unit = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
  if (n == 0) ()
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  else if (visited.contains(url)) () //println("Already visited: " + n + " " + url)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
  else {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
    println("Visiting: " + n + " " + url);
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
    visited += url
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
    for (u <- getAllURLs(getURLpage(url))) crawl(u, n - 1);
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
  }
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
}