Skip to content

Instantly share code, notes, and snippets.

@jeanpaulrsoucy
Created April 12, 2021 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanpaulrsoucy/5185660d7471a4f4cd6ad6647731d894 to your computer and use it in GitHub Desktop.
Save jeanpaulrsoucy/5185660d7471a4f4cd6ad6647731d894 to your computer and use it in GitHub Desktop.

Scrape COVID-19 pharmacy vaccine locations for Ontario

Jean-Paul R. Soucy (jprs.me)

Let's scrape the names, addresses and public health units of pharmacies in Ontario offering the COVID-19 vaccine from the provincial website.

# load packages
library(rvest)
library(stringr)

# load webpage and list pharmacy cards
webpage <- read_html("https://covid-19.ontario.ca/vaccine-locations") %>%
  html_nodes(xpath = '//*[@class="ontario-assessment-centre-card__wrapper"]')

# number of pharmacies
n_pharms <- length(webpage)

# create table
pharms <- data.frame(
  name = character(n_pharms),
  address_street = character(n_pharms),
  address_city = character(n_pharms),
  address_province = character(n_pharms),
  address_postal_code = character(n_pharms),
  address_country = character(n_pharms),
  phu = character(n_pharms)
)

# extract name, address and PHU from each each pharmacy card
for (i in 1:length(webpage)) {
  
  # ## print progress
  # cat(i, fill = TRUE)
  
  ## extract data
  dat <- webpage[i] %>%
    html_elements("p")
  
  ## fill in table
  pharms[i, "name"] <- dat[1] %>% html_text
  pharms[i, "address_street"] <- dat[2] %>% html_text
  pharms[i, "address_city"] <- dat[3] %>% html_text %>% str_sub(0, -5)
  pharms[i, "address_province"] <- "Ontario"
  pharms[i, "address_postal_code"] <- dat[4] %>% html_text
  pharms[i, "address_country"] <- "Canada"
  pharms[i, "phu"] <- dat[length(dat)] %>% html_text
  
}

# write table
write.csv(pharms, "pharms.csv", row.names = FALSE)

Previewing the results:

head(pharms)
##                   name                address_street address_city address_province
## 1      Costco Pharmacy        150 Kingston Road East         Ajax          Ontario
## 2 Health-Rite Pharmacy          75 Bayly Street West         Ajax          Ontario
## 3      Loblaw Pharmacy         30 Kingston Road West         Ajax          Ontario
## 4               Rexall 955 Westney Road South Unit 7         Ajax          Ontario
## 5   Shoppers Drug Mart         15 Westney Road North         Ajax          Ontario
## 6               Rexall              149 Young Street     Alliston          Ontario
##   address_postal_code address_country                                 phu
## 1             L1Z 1E5          Canada     Durham Region Health Department
## 2             L1S 7K7          Canada     Durham Region Health Department
## 3             L1T 4K8          Canada     Durham Region Health Department
## 4             L1S 3K7          Canada     Durham Region Health Department
## 5             L1T 1P4          Canada     Durham Region Health Department
## 6             L9R 2A9          Canada Simcoe Muskoka District Health Unit

The number of pharmacies by Public Health Unit:

table(pharms$phu) %>%
  as.data.frame
##                                                      Var1 Freq
## 1                               Algoma Public Health Unit    3
## 2                                Brant County Health Unit    3
## 3                                Chatham-Kent Health Unit    3
## 4                         Durham Region Health Department   25
## 5                             Eastern Ontario Health Unit    3
## 6                                  Grey-Bruce Health Unit    3
## 7                           Haldimand-Norfolk Health Unit    3
## 8   Haliburton, Kawartha, Pine Ridge District Health Unit    5
## 9                         Halton Region Health Department   19
## 10                        Hamilton Public Health Services   21
## 11        Hastings and Prince Edward Counties Health Unit    3
## 12                                Huron Perth Health Unit    3
## 13 Kingston, Frontenac and Lennox & Addington Health Unit   46
## 14                                    Lambton Health Unit    3
## 15       Leeds,Grenville, and Lanark District Health Unit    2
## 16                           Middlesex-London Health Unit    2
## 17                Niagara Region Public Health Department   22
## 18             North Bay-Parry Sound District Health Unit    4
## 19                               Northwestern Health Unit    3
## 20                                   Ottawa Public Health   34
## 21                                     Peel Public Health   42
## 22                             Peterborough Public Health    3
## 23                                  Porcupine Health Unit    3
## 24                      Region of Waterloo, Public Health   19
## 25                Renfrew County and District Health Unit    3
## 26                    Simcoe Muskoka District Health Unit   25
## 27                             Southwestern Public Health    5
## 28                                                Sudbury    3
## 29                                            Thunder Bay    3
## 30                                            Timiskaming    3
## 31                                  Toronto Public Health  275
## 32                 Wellington-Dufferin-Guelph Health Unit   10
## 33                       Windsor-Essex County Health Unit   57
## 34                     York Region Public Health Services   38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment