knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

scrapex

Since web scraping and API tutorials are deemed to be obsolete once they changes it’s structure or go down indefinately, scrapex saves specific web paths of certain websites to make scraping examples reproducibly in the long term. Moreover, it scrapex contains local copies of custom APIs for educational purposes using the plumber package. To make the package as lightweight as possible, for the local copies of websites, some images/figures/maps have been excluded. Aside from that, most examples should contain the rough skeleton of the original website.

Installation

scrapex will probably never see the light of CRAN since it is deemed to grow in size if new examples come along, so you need to install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("cimentadaj/scrapex")

Website Example

scrapex contains websites which have been saved locally for users to use as examples. Each example has only one function which returns the local links to the html websites. For example, for the spanish schools examples (see ?spanish_schools_ex for documentation on the example), we can obtain the links with spanish_schools_ex():

library(scrapex)
library(xml2)

school_websites <- spanish_schools_ex()
school_websites

To browse the actual website of one of these links, use prep_browser with openURL:

browseURL(prep_browser(school_websites)[1])

To actually scrape information from these websites, they work as expected with read_html.

# You can read them with `read_html`
read_html(school_websites[1])

API example

All APIs in scrapex export a function that launches the API. These functions start with api_*. For example, loading the local copy of the API for the public bicycle system of Barcelona can be done like this:

library(scrapex)
library(httr2)
bicing <- api_bicing()

You'll see that the API returns a link to the documentation of the API as well as to the main URL of the API. Users can then make requests just as if this was a real API. For example:

rt_bicing <- paste0(bicing$api_web, "/api/v1/real_time_bicycles")
resp_output <- 
  rt_bicing %>%
  request() %>% 
  req_perform() %>%
  resp_body_json() %>%
  lapply(data.frame) %>%
  do.call(rbind, .)

resp_output

Collaboration

This package is a work in progress and welcomes more examples from different backgrounds. If you want to contribute to the package you can send a pull request following these guidelines:

For webscraping, I encourage anyone looking to do a PR to look at the one for spanish_schools_ex which is called download_school.R (note that this is tricky because downloading a path of a website many times results in downloading the complete website. Look at the example in download_school.R to only download single paths). For the API example, I encourage users to look at the file R/api_coveragedb.R and inst/plumber/api_coveragedb/.

All suggestions are very welcome if you feel this is not the most optimal storage strategy! Open an issue request and we'll discuss it.



cimentadaj/scrapex documentation built on Jan. 5, 2023, 9:49 p.m.