author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Wed, 25 Sep 2013 20:35:54 +0100 | |
changeset 99 | 91145f6d9b0e |
parent 93 | 4794759139ea |
child 103 | bea2dd1c7e73 |
permissions | -rw-r--r-- |
1 | 1 |
import java.io.OutputStreamWriter |
2 |
import java.net.URL |
|
3 |
import scala.io.Source.fromInputStream |
|
4 |
||
5 |
val url = new URL("http://www.envir.gov.cn/eng/airep/index.asp") |
|
6 |
||
7 |
//connect to url |
|
8 |
val conn = url.openConnection |
|
9 |
conn.setRequestProperty("User-Agent", "") |
|
10 |
conn.setDoOutput(true) |
|
11 |
conn.connect |
|
12 |
||
13 |
//sending data |
|
14 |
val wr = new OutputStreamWriter(conn.getOutputStream()) |
|
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
15 |
//wr.write("Fdate=2012-9-24&Tdate=2013-08-25") |
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
16 |
//wr.write("Fdate=2012-9-18&Tdate=2012-09-24") |
1 | 17 |
wr.write("Fdate=2001-5-18&Tdate=2012-09-25") |
18 |
wr.flush |
|
19 |
wr.close |
|
20 |
||
21 |
//receiving data |
|
22 |
val page = fromInputStream(conn.getInputStream).getLines.mkString("\n") |
|
23 |
||
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
24 |
println(page) |
1 | 25 |
|
26 |
// regular expression . excludes newlines, |
|
27 |
// therefore we have to use [\S\s] |
|
28 |
val regex1 = """<tr align="center">[\S\s]*?</tr>""".r |
|
29 |
val rows = regex1.findAllIn(page).toList |
|
30 |
||
7 | 31 |
//print(rows) |
1 | 32 |
|
33 |
val regex2 = """<td align="center">([\S\s]*?)</td>""".r |
|
34 |
||
35 |
def aux(s: String) : Array[String] = { |
|
36 |
for (m <- regex2.findAllIn(s).toArray) yield m match { |
|
37 |
case regex2(value) => value.trim |
|
38 |
} |
|
39 |
} |
|
40 |
||
41 |
val data = rows.map { aux } |
|
42 |
||
43 |
def compare(i: Int)(e: Array[String], f: Array[String]) = e(i).toInt < f(i).toInt |
|
44 |
||
45 |
//day with highest particle pollution (PM_10) |
|
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
46 |
println(data.sortWith(compare(1)).last) |
1 | 47 |
|
48 |
//day with highest sulfur dioxide (SO_2) |
|
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
49 |
println(data.sortWith(compare(2)).last) |
1 | 50 |
|
51 |
//day with highest nitro dioxide (NO_2) |
|
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
52 |
println(data.sortWith(compare(3)).last) |
1 | 53 |
|
54 |
//days with highest PM_10 |
|
55 |
val groups = data.groupBy(_(1).toInt) |
|
56 |
val max_key = groups.keySet.max |
|
99
91145f6d9b0e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
57 |
println(groups(max_key)) |