author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 26 Sep 2013 15:11:25 +0100 | |
changeset 108 | 52ee218151f9 |
parent 101 | 4758a6155878 |
child 112 | 95ee5cc5c05d |
permissions | -rw-r--r-- |
100
cbc2270c2938
updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
99
diff
changeset
|
1 |
// This version of the crawler also |
cbc2270c2938
updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
99
diff
changeset
|
2 |
// harvests emails from webpages |
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 |
|
101
4758a6155878
links
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
8 |
// gets the first ~10K of a web-page |
99
91145f6d9b0e
added
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"); ""} |
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
96
diff
changeset
|
12 |
} |
7 | 13 |
|
14 |
// staring URL for the crawler |
|
15 |
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc/""" |
|
16 |
||
17 |
// regex for URLs |
|
18 |
val http_pattern = """\"https?://[^\"]*\"""".r |
|
19 |
val my_urls = """urbanc""".r |
|
20 |
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r |
|
21 |
||
100
cbc2270c2938
updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
99
diff
changeset
|
22 |
// The regular expression for emails comes from: |
cbc2270c2938
updated progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
99
diff
changeset
|
23 |
// http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/ |
7 | 24 |
|
25 |
def unquote(s: String) = s.drop(1).dropRight(1) |
|
26 |
||
27 |
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
|
28 |
http_pattern.findAllIn(page).map(unquote).toSet |
7 | 29 |
} |
30 |
||
31 |
// naive version - seraches until a given depth |
|
32 |
// visits pages potentially more than once |
|
33 |
def crawl(url: String, n: Int) : Unit = { |
|
34 |
if (n == 0) () |
|
35 |
//else if (my_urls.findFirstIn(url) == None) () |
|
36 |
else { |
|
95
dbe49327b6c5
added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
37 |
println(s"Visiting: $n $url") |
7 | 38 |
val page = get_page(url) |
39 |
println(email_pattern.findAllIn(page).mkString("\n")) |
|
40 |
for (u <- get_all_URLs(page)) crawl(u, n - 1) |
|
41 |
} |
|
42 |
} |
|
43 |
||
44 |
crawl(startURL, 3) |