-
Notifications
You must be signed in to change notification settings - Fork 14
/
images_scrap.R
140 lines (92 loc) · 2.99 KB
/
images_scrap.R
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#' Scrape Images from a Web Page
#'
#' @param link the link of the web page
#' @param imgpath the path of the images. Defaults to the current directory
#' @param extn the extension of the image: png, jpeg ...
#' @param askRobot logical. Should the function ask the robots.txt if we're allowed or not to scrape the web page ? Default is FALSE.
#'
#' @return Images
#'
#' @examples \dontrun{
#'
#' images_scrap(link = "https://rstudio.com/", extn = "png")
#'
#' }
#'
#' @export
#' @importFrom rvest html_nodes html_attr %>%
#' @importFrom xml2 read_html
#' @importFrom robotstxt paths_allowed
#' @importFrom crayon green
#' @importFrom crayon bgRed
#' @importFrom curl has_internet
#' @importFrom utils download.file
images_scrap <- function(link,
imgpath = getwd(),
extn,
askRobot = FALSE) {
if (missing(link)) {
stop("'link' is a mandatory parameter")
}
if (missing(extn)) {
stop("You need to provide the extension of the required images:
jpg, png, jpeg, ...")
}
if (!is.character(link) ||
!is.character(imgpath)||
!is.character(extn)) {
stop("'link', 'imgpath' and 'extn' parameters must
be provided as character strings")
}
if (imgpath != getwd() && !dir.exists(imgpath)) {
stop("the path: ", imgpath, " doesn't seem to exist !")
}
if (grepl(x = extn, pattern = "\\.")) {
stop("No need to include the '.' in 'extn',
just provide the extension as it is")
}
###################### Ask robot related ##################################################
if (askRobot) {
if (paths_allowed(link) == TRUE) {
message(green("the robot.txt doesn't prohibit scraping this web page"))
} else {
message(bgRed(
"WARNING: the robot.txt doesn't allow scraping this web page"
))
}
}
##########################################################################################
tryCatch(
expr = {
img_urls <- lapply(link, function(url) {
url %>%
read_html() %>%
html_nodes("img") %>%
html_attr("src")
})
img_urls_unlist <- img_urls %>% unlist()
img_urls_f <- img_urls_unlist[grepl(pattern = paste0(".", extn),
x = img_urls_unlist,
ignore.case = FALSE)]
for (i in seq_along(img_urls_f)) {
download.file(img_urls_f[i],
destfile = basename(img_urls_f[i]),
mode = "wb")
}
},
error = function(cond) {
if (!has_internet()) {
message(paste0("Please check your internet connexion: ", cond))
return(NA)
} else if (grepl("current working directory", cond) ||
grepl("HTTP error 404", cond)) {
message(paste0("The URL doesn't seem to be a valid one: ", link))
message(paste0("Here the original error message: ", cond))
return(NA)
} else {
message(paste0("Undefined Error: ", cond))
return(NA)
}
}
)
}