author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sat, 13 Sep 2014 04:30:25 +0100 | |
changeset 242 | 35104ee14f87 |
parent 112 | 95ee5cc5c05d |
child 254 | dcd4688690ce |
permissions | -rw-r--r-- |
112
95ee5cc5c05d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
101
diff
changeset
|
1 |
// This version of the crawler that also |
95ee5cc5c05d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
101
diff
changeset
|
2 |
// "harvests" email addresses from webpages |
100
cbc2270c2938
updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
99
diff
changeset
|
3 |
|
7 | 4 |
import io.Source |
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._ |
7 | 7 |
|
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
96
diff
changeset
|
8 |
def get_page(url: String) : String = { |
96
9fcd3de53c06
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
95
diff
changeset
|
9 |
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
|
10 |
{ println(s" Problem with: $url"); ""} |
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
96
diff
changeset
|
11 |
} |
7 | 12 |
|
112
95ee5cc5c05d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
101
diff
changeset
|
13 |
// regexes for URLs, for "my" domain and for email addresses |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
112
diff
changeset
|
14 |
val http_pattern = """"https?://[^"]*"""".r |
7 | 15 |
val my_urls = """urbanc""".r |
16 |
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r |
|
17 |
||
18 |
def unquote(s: String) = s.drop(1).dropRight(1) |
|
19 |
||
20 |
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
|
21 |
http_pattern.findAllIn(page).map(unquote).toSet |
7 | 22 |
} |
23 |
||
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
112
diff
changeset
|
24 |
def print_str(s: String) = |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
112
diff
changeset
|
25 |
if (s == "") () else println(s) |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
112
diff
changeset
|
26 |
|
7 | 27 |
def crawl(url: String, n: Int) : Unit = { |
28 |
if (n == 0) () |
|
29 |
else { |
|
95
dbe49327b6c5
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
30 |
println(s"Visiting: $n $url") |
7 | 31 |
val page = get_page(url) |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
112
diff
changeset
|
32 |
print_str(email_pattern.findAllIn(page).mkString("\n")) |
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
112
diff
changeset
|
33 |
for (u <- get_all_URLs(page).par) crawl(u, n - 1) |
7 | 34 |
} |
35 |
} |
|
36 |
||
112
95ee5cc5c05d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
101
diff
changeset
|
37 |
// staring URL for the crawler |
242
35104ee14f87
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
112
diff
changeset
|
38 |
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc""" |
112
95ee5cc5c05d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
101
diff
changeset
|
39 |
|
7 | 40 |
crawl(startURL, 3) |