The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis You’ll need to start by signing up for an API key. Your task is to choose one of the New York Times APIs, construct an interface in Python to read in the JSON data, and transform it into a pandas DataFrame.
For this assignment, I chose to connect to the New York Times’ Article Search API. I queried the API for articles relating to vaping deaths, and limited the search results to articles relating to the keyword “Death”.
library(jsonlite)
library(tidyr)
library(dplyr)
library(stringr)
library(kableExtra)
api_key <- "&api-key=sm2AdtUSMtxAW5ZDlRLYAAGyYMrYGTnn"
search_query <- "?q=vaping%20deaths"
# Define filters to limit the search results.
filters <- "&fq=document_type(article)&fq=type_of_material(News)&fq=keyword(Deaths (Fatalities))"
sort_by <- "&sort=relevance"
start <- "&begin_date=20190101"
end <- "&end_date=20191026"
article_endpoint <- "https://api.nytimes.com/svc/search/v2/articlesearch.json"
vaping_articles <- fromJSON(paste0(article_endpoint, search_query, filters, start, end, sort_by, api_key))
results <- vaping_articles$response$docs
vaping_df <- data.frame('Date' = format(as.Date(results$pub_date), "%B %d, %Y"),
'Headline' = results$headline$main,
'Abstract' = results$abstract,
'URL' = results$web_url)
vaping_df %>% kable() %>% kable_styling() %>% scroll_box(width = "910px", height = "400px")
| Date | Headline | Abstract | URL |
|---|---|---|---|
| September 16, 2019 | Amid Vaping Deaths, California Targets Counterfeit Products | The move comes as a scourge of vaping-related illnesses across the country has killed at least seven people. | https://www.nytimes.com/2019/09/16/us/california-vaping.html |
| October 08, 2019 | Bronx Teenager’s Death Is the Youngest Vaping Fatality in U.S. | The 17-year-old who died last week is the youngest of 23 people to die nationwide of a vaping-related lung illness. | https://www.nytimes.com/2019/10/08/nyregion/vaping-death.html |
| August 23, 2019 | First Death in a Spate of Vaping Sicknesses Reported by Health Officials | An Illinois patient has died following a respiratory illness linked to vaping. Health officials offered few details about the circumstances. | https://www.nytimes.com/2019/08/23/health/vaping-death-cdc.html |
| October 10, 2019 | Vaping Illnesses Climb Upward, Nearing 1,300 With 29 Deaths | The cause of the outbreak is still unknown, and the only advice health officials can offer so far is to avoid vaping. | https://www.nytimes.com/2019/10/10/health/vaping-illnesses-cdc.html |
| October 14, 2019 | He Tried E-Cigarettes to Quit Smoking. Doctors Say Vaping Led to His Death. | Months after John Steffen died in Nebraska, officials said his death was part of a mysterious outbreak of vaping-related deaths and illnesses. | https://www.nytimes.com/2019/10/14/us/vaping-victim.html |
| October 01, 2019 | Vaping Illness Tracker: 1,604 Cases and 34 Deaths | Hundreds of people have been sickened by lung illnesses linked to vaping, most by using THC products. | https://www.nytimes.com/interactive/2019/health/vaping-illness-tracker.html |
| October 16, 2019 | Officials Say Death of Montana Teenager Was Vaping-Related | Health officials say a teenager in Montana has died of a lung disease associated with a national outbreak of vaping-related illnesses. | https://www.nytimes.com/aponline/2019/10/16/us/ap-us-vaping-death-montana.html |
| October 18, 2019 | Factbox: U.S. Vaping-Related Deaths Rise to 33 | U.S. health officials on Thursday reported 33 deaths and 1,479 confirmed and probable cases so far from a mysterious respiratory illness tied to vaping. | https://www.nytimes.com/reuters/2019/10/17/us/17reuters-health-vaping-cdc-factbox.html |
| September 04, 2019 | Another Patient Has Died From Lung Disease After Vaping | Health authorities in Oregon are investigating the case but would not identify the age of the patient or the brand of the device used. | https://www.nytimes.com/2019/09/04/health/vaping-death-lung.html |
| September 26, 2019 | Vaping-Related Illnesses Climb to 805, C.D.C. Says | The latest weekly tally includes 275 more reports of patients sickened, in 46 states. There are now 12 deaths linked to vaping-related lung injuries. | https://www.nytimes.com/2019/09/26/health/vaping-illnesses-cdc.html |