Vivek Katial
10/11/17
We will be looking at different ways to get data off the web. There are 2 ways
read.csv("example.csv")
publicid origintime longitude latitude depth
1 2014p172810 2014-03-05T22:33:37 175.93 -36.91 368.75
2 2014p172742 2014-03-05T21:57:26 175.89 -40.60 22.46
3 2014p172698 2014-03-05T21:33:31 175.86 -40.63 27.44
We have a variation of paste, but rather than pasting in 1 value we are actually pasting in each of the elements within country.
countries <- c("NZ1", "AUS", "US1")
urls <- sprintf("https://www.stat.auckland.ac.nz/~paul/stats769/2017/AC34/20130901103549-NAV-%s.csv", countries)
urls
[1] "https://www.stat.auckland.ac.nz/~paul/stats769/2017/AC34/20130901103549-NAV-NZ1.csv"
[2] "https://www.stat.auckland.ac.nz/~paul/stats769/2017/AC34/20130901103549-NAV-AUS.csv"
[3] "https://www.stat.auckland.ac.nz/~paul/stats769/2017/AC34/20130901103549-NAV-US1.csv"
races <- lapply(urls, read.csv)
Accessing a resource on the web can be more more difficult.
We will investigate R packages that do this!
The httr package allows you to request a URL and supply a username and password.
library(httr)
GET("https://www.stat.auckland.ac.nz/~paul/")
Response [https://www.stat.auckland.ac.nz/~paul/]
Date: 2017-11-10 09:50
Status: 200
Content-Type: text/html; charset=UTF-8
Size: 27.3 kB
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Paul Murrell's Home Page</title>
<STYLE TYPE="text/css">
BODY { padding-left: 100; padding-right: 100 }
</STYLE>
...
The content type informs us the format of the response used int this.
response <- GET("http://httpbin.org/basic-auth/user/passwd",
authenticate("user", "passwd"))
There is another function in the httr package which cleans stuff up for us aswell.
content(response, as = "parsed")
$authenticated
[1] TRUE
$user
[1] "user"
A web page has some elements that you can fill in to get the data you want. How do you get this data? If you need to interact with a GUI, there may be difficulty to download stuff from a GUI.
When we search for STATS 769 on google we get the following URL.
We can see ?, &, +, = making some sort of pattern in this URL.
This indicates, we can search whatever we want using Google if we can generate these URLs. This is how GET requests work.
flights <- GET("http://www.rita.dot.gov/bts/acts/customized/table?adfy=2016&adfm=1&adty=2016&adtm=4&aos=0&artd=1&arti&arts&asts=1&astns&astt&ascc&ascp=1")
flights
Response [https://www.rita.dot.gov/bts/acts/customized/table?adfy=2016&adfm=1&adty=2016&adtm=4&aos=0&artd=1&arti&arts&asts=1&astns&astt&ascc&ascp=1]
Date: 2017-11-10 09:50
Status: 200
Content-Type: text/html; charset=utf-8
Size: 46.1 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" version="XHTML+...
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/terms/"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:og="http://ogp.me/ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:sioc="http://rdfs.org/sioc/ns#"
xmlns:sioct="http://rdfs.org/sioc/types#"
...
There are another type of forms called POST forms. POST rquests allow us to include additional information in the body of the request.
# POST a form
# Demonstrate interactive use of
# http://fred.stlouisfed.org/series/TOTALSA/downloaddata
# (which unfortunately is no longer a POST form :()
# so use "dummy" form instead
url <- "https://www.stat.auckland.ac.nz/~paul/stats769/2017/Form/output.php"
query <- list(count="5")
response <- POST(url, body = query)
content(response, as="text")
[1] "<html>\n<body>\nTAILS<br>TAILS<br>TAILS<br>HEADS<br>TAILS<br></body>\n</html>\n"
If web servers don't like someone making a shit tonne of requests, you may end up being banned from the web server! Or your IP Address maybe banned ;-)
e.g. www.worldrugby.org/results
If we did a GET from the httr package on this web page we would end up with something else.
We use the package rdom. We need phantomjs also on the machine. This isn't really in the scope of the course. BUT, remember this!!
Web APIs are designed to share information with a programmer, rather than having someone reverse-engineer an interface.