-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added washingtonpost.com scraper (#1)
- Loading branch information
JBGruber
committed
Aug 26, 2021
1 parent
8383a67
commit 3d97232
Showing
6 changed files
with
99 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package: paperboy | ||
Title: Comprehensive collection of news media scrapers | ||
Version: 0.0.1.9000 | ||
Date: 2021-08-23 | ||
Date: 2021-08-26 | ||
Authors@R: person("Johannes", "Gruber", email = "[email protected]", | ||
role = c("aut", "cre")) | ||
Description: A comprehensive collection of webscraping scripts for news media sites. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
pb_deliver_paper.www_washingtonpost_com <- function(x, verbose = NULL, ...) { | ||
|
||
. <- NULL | ||
|
||
if (is.null(verbose)) verbose <- getOption("paperboy_verbose") | ||
|
||
if (!"tbl_df" %in% class(x)) | ||
stop("Wrong object passed to internal deliver function: ", class(x)) | ||
|
||
if (verbose) message("\t...", nrow(x), " articles from ", x$domain[1]) | ||
|
||
pb <- make_pb(x) | ||
|
||
purrr::map_df(seq_along(x$url), function(i) { | ||
if (basename(x$expanded_url[i]) == x$domain[i]) { | ||
tibble::tibble( | ||
datetime = NA, | ||
author = NA, | ||
headline = NA, | ||
text = NA | ||
) | ||
} else { | ||
|
||
cont <- x$content_raw[i] | ||
if (verbose) pb$tick() | ||
|
||
html <- rvest::read_html(cont) | ||
|
||
# datetime | ||
suppressWarnings( | ||
datetime <- html %>% | ||
rvest::html_elements("[property=\"article:published_time\"],[itemprop*=\"datePublished\"],[name=\"ga-publishDate\"]") %>% | ||
rvest::html_attr("content") %>% | ||
lubridate::as_datetime() | ||
) | ||
|
||
if (length(datetime) < 1) { | ||
datetime <- html %>% | ||
rvest::html_elements("[class*=\"date\"]") %>% | ||
rvest::html_text() %>% | ||
strptime(format = "%B %d, %Y | %I:%M %p") | ||
} | ||
|
||
|
||
# headline | ||
headline <- html %>% | ||
rvest::html_elements("[property=\"og:title\"]") %>% | ||
rvest::html_attr("content") | ||
|
||
# author | ||
author <- html %>% | ||
rvest::html_elements("[data-qa=\"author-name\"],[class*=\"author-name \"]") %>% | ||
rvest::html_text2() %>% | ||
toString() | ||
|
||
# text | ||
text_temp <- html %>% | ||
rvest::html_elements("[class=\"article-body\"]") | ||
|
||
if (length(text_temp) > 0) { | ||
text <- text_temp %>% | ||
rvest::html_elements("p") %>% | ||
rvest::html_text2() %>% | ||
paste(collapse = "\n") | ||
} else { | ||
text <- html %>% | ||
rvest::html_elements("p") %>% | ||
rvest::html_text2() %>% | ||
paste(collapse = "\n") | ||
} | ||
|
||
tibble::tibble( | ||
datetime, | ||
author, | ||
headline, | ||
text | ||
) | ||
} | ||
}) %>% | ||
cbind(x) %>% | ||
normalise_df() %>% | ||
return() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters