-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNews from API.Rmd
68 lines (47 loc) · 1.65 KB
/
News from API.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
---
title: "Data Collection from News API"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
**Step 1- Signup for an API key at https://newsapi.org/account**
Refer to accessible endpoints - https://newsapi.org/docs/endpoints
**Step 2 - Load required packages.**
We will need `httr`, `RCurl` and `jsonlite` packages in the R Workspace.
```{r, setup}
suppressPackageStartupMessages({
if (!require(httr)) {install.packages("httr")}; library(httr)
if (!require(RCurl)) {install.packages("RCurl")}; library(RCurl)
if (!require(jsonlite)) {install.packages("jsonlite")}; library(jsonlite)
})
```
**Step 3 - As mentioned in doc https://newsapi.org/docs/endpoints/everything**
** Hit endpoint with search parameter and api key**
```{r querying business analytics news}
search.term <- "business%20analytics"
api.key <- "8d88c21f6fd7423b9c84c38f78d84703"
page.size <- 100
url <- paste0("https://newsapi.org/v2/everything?q=", search.term, "&apiKey=", api.key, "&pageSize=", page.size)
print(url)
system.time({
all.news <- getURL(url)
})
news.json <- jsonlite::fromJSON(all.news)
```
### Iterate through news results
```{r}
source.id <- news.json$articles$source$id
source.name <- news.json$articles$source$name
author <- news.json$articles$author
title <- news.json$articles$title
description <- news.json$articles$description
content <- news.json$articles$content
news.df <- data.frame(source.id, source.name, author, title, description, content)
headers <- c("Source Id", "Source Name", "Author", "Title", "Description", "Content")
colnames(news.df) <- headers
head(news.df)
```
```{r}
write.csv(news.df, "output_api.csv")
```