Get startedGet started for free

Build a function

You're still trying to perfect your tools for doing webs scraping to be as efficient as possible doing your job as a data analyst for a web agency.

In this exercise, you will make the extractor function from the previous exercise a little bit stricter: if the code returned by the status extractor is not between 200 and 203, the function will return a missing value (NA). In the other case, the status code will be returned.

purrr and httr have been loaded for you.

This exercise is part of the course

Intermediate Functional Programming with purrr

View Course

Exercise instructions

  • Negate the %in% operator, which is used to test if the element on the left is inside the element of the right.

  • Compose a extract_status() function, which will be a combination of GET() and status_code().

  • Complete the given function: the url status code should be extracted and assigned to a code variable. Then if this code is not in 200:203, a missing value will be returned. Otherwise, the status code is returned.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Negate the %in% function 
`%not_in%` <- ___(`%in%`)

# Compose a status extractor 
extract_status <- ___(___, ___)

# Complete the function definition
strict_code <- function(url) {
  # Extract the status of the URL
  code <- ___(___)
  # If code is not in the acceptable range ...
  if (code ___ 200:203) {
    # then return NA
    return(___)
  }
  code
}
Edit and Run Code