Exercise

Safe iterations

As in the previous chapter, let's pretend you are a data analyst working for a web agency. This time, you've been asked to do some web scraping.

(Note: don't be afraid if you don't know how to do web scraping, we'll start simple, and all the functions will be explained).

You have received a list of URLs, but you suspect that some are not real addresses. The first thing you will do is test if you can connect to these URLs. For this, we'll use a simple function from the readr package: read_lines(), that we will put inside a safely(). When given an URL, read_lines() reads the HTML, or returns an error if the URL is not reachable.

Theurls vector is available in your workspace. Print it in the console if you want to know what is inside.

Instructions

100 XP
  • Create a safe version of the read_lines() function.

  • Map this newly created function of the provided vector called urls.

  • Set the names of the results with the set_names() function.

  • Extract the "error" element of each sublist.