progs/scraper.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 26 Sep 2013 10:52:54 +0100
changeset 103 bea2dd1c7e73
parent 99 91145f6d9b0e
child 114 735f7bbfae9b
permissions -rw-r--r--
links
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     1
// A Web-Scraper that extracts the daily Shanghai polution
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     2
// data from the web-page
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     3
//
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     4
//   http://www.envir.gov.cn/eng/airep/index.asp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     5
//
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     6
//
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     7
// Important! They stoped providing this data in November
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     8
// 2012, but kept the historical data since 2001. So dates
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
     9
// must be in that range.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    10
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
import java.io.OutputStreamWriter
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
import java.net.URL
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
import scala.io.Source.fromInputStream
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
val url = new URL("http://www.envir.gov.cn/eng/airep/index.asp")
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    17
//connecting to url
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
val conn = url.openConnection
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
conn.setRequestProperty("User-Agent", "")
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
conn.setDoOutput(true)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
conn.connect
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
//sending data
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
val wr = new OutputStreamWriter(conn.getOutputStream())
103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    25
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    26
//possible date ranges
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    27
wr.write("Fdate=2012-9-24&Tdate=2012-08-25")
99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    28
//wr.write("Fdate=2012-9-18&Tdate=2012-09-24")
103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    29
//wr.write("Fdate=2001-5-18&Tdate=2012-09-25")
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
wr.flush
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
wr.close
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
//receiving data
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
val page = fromInputStream(conn.getInputStream).getLines.mkString("\n")
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    36
//data encoded as a string, which you can see with
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 99
diff changeset
    37
//println(page)
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// regular expression . excludes newlines, 
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
// therefore we have to use [\S\s]
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
val regex1 = """<tr align="center">[\S\s]*?</tr>""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
val rows = regex1.findAllIn(page).toList
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents: 1
diff changeset
    44
//print(rows)
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
val regex2 = """<td align="center">([\S\s]*?)</td>""".r
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
def aux(s: String) : Array[String] = {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  for (m <- regex2.findAllIn(s).toArray) yield m match {
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    case regex2(value) => value.trim
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  }
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
val data = rows.map { aux }
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
def compare(i: Int)(e: Array[String], f: Array[String]) = e(i).toInt < f(i).toInt
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
//day with highest particle pollution (PM_10)
99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    59
println(data.sortWith(compare(1)).last)
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
//day with highest sulfur dioxide (SO_2)
99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    62
println(data.sortWith(compare(2)).last)
1
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
//day with highest nitro dioxide (NO_2)
99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    65
println(data.sortWith(compare(3)).last)
1
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
//days with highest PM_10
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
val groups = data.groupBy(_(1).toInt)
b606c9439fa6 new version
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
val max_key = groups.keySet.max
99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    70
println(groups(max_key))