R从数千页中抓取图像细节



我正试图从一个网站上抓取细节,以便在r中收集脚本图片的细节。

我需要的是:

  • 图片名称(1.jpg)
  • 图片说明("一名新兵示范正确使用二氧化碳便携式灭火器扑灭小型室外火灾。")
  • 图片来源(图片由:James fortner提供)

有超过16,000个文件,值得庆幸的是网址是&;…asp?Photo = 1,2,3,4 "有base url,它不会改变,只是最后一个带有图像编号的部分。我想脚本的循环无论是一组数字(我告诉它从哪里开始),或者它只是打破,当它到达一个不存在的页面。

使用下面的代码,我可以得到照片的标题,但只有一行。我想要拍照,这是在另一条线上;在主标题和照片名称之间有三个
。如果生成的表有两个或三个空白列来说明
行,我就可以了,因为我可以稍后删除它们。

library(rvest)
library(dplyr)
link = "http://fallschurchvfd.org/photovideo.asp?photo=1"
page = read_html(link)
caption = page %>% html_nodes(".text7 i") %>% html_text()
info = data.frame(caption, stringsAsFactors = FALSE)
write.csv(info, "photos.csv")

rvesttidyverse刮痧

library(tidyverse)
library(rvest)
get_picture <- function(page) {
cat("Scraping page", page, "n")

page <- str_c("http://fallschurchvfd.org/photovideo.asp?photo=", page) %>%
read_html()

tibble(
image_name = page %>%  
html_element(".text7 img") %>%
html_attr("src"),
caption = page %>%
html_element(".text7") %>%
html_text() %>%
str_split(pattern = "rntttt") %>%
unlist %>% 
nth(1),
credit = page %>%
html_element(".text7") %>%
html_text() %>%
str_split(pattern = "rntttt") %>%
unlist %>% 
nth(3)
)
}
# Get the first 1:50 
df <- map_dfr(1:50, possibly(get_picture, otherwise = tibble()))
# A tibble: 42 × 3
image_name     caption                                   credit
<chr>          <chr>                                     <chr> 
1 /photos/1.jpg  Recruit Clay Hamric demonstrates the use… James…
2 /photos/2.jpg  A recruit demonstrates the proper use of… James…
3 /photos/3.jpg  Recruit Paul Melnick demonstrates the pr… James…
4 /photos/4.jpg  Rescue 104                                James…
5 /photos/5.jpg  Rescue 104                                James…
6 /photos/6.jpg  Rescue 104                                James…
7 /photos/15.jpg Truck 106 operates a ladder pipe from Wi… Jim O…
8 /photos/16.jpg Truck 106 operates a ladder pipe as heav… Jim O…
9 /photos/17.jpg Heavy fire vents from the roof area of t… Jim O…
10 /photos/18.jpg Arlington County Fire and Rescue Associa… James…
# … with 32 more rows
# ℹ Use `print(n = ...)` to see more rows

对于图像,可以使用命令行工具curl。例如,通过100.jpg

下载图像1.jpg
curl -O "http://fallschurchvfd.org/photos/[0-100].jpg"

对于R代码,如果您抓取整个.text7部分,那么您可以随后拆分为标题和照片credit:

extractedtext <- page %>% html_nodes(".text7") %>% html_text()
caption <- str_split(extractedtext, "rntttt")[[1]][1]
credit <- str_split(extractedtext, "rntttt")[[1]][3]

作为循环

library(rvest)
library(tidyverse)
df<-data.frame(id=1:20, 
image=NA,
caption=NA,
credit=NA)
for (i in 1:20){
cat(i, " ") # to monitor progress and debug
link <- paste0("http://fallschurchvfd.org/photovideo.asp?photo=", i)
tryCatch({ # This is to avoid stopping on an error message for missing pages
page <- read_html(link)
close(link)
df$image[i] <- page %>% html_nodes(".text7 img") %>% html_attr("src")
extractedtext <- page %>% html_nodes(".text7") %>% html_text()
df$caption[i] <- str_split(extractedtext, "rntttt")[[1]][1] # This is an awkward way of saying "list 1, element 1"
df$credit[i] <- str_split(extractedtext, "rntttt")[[1]][3]
}, 
error=function(e){cat("ERROR :",conditionMessage(e), "n")})
}

我得到与当前代码不一致的结果,例如,第15页比第1页有更多的换行符。

TODO:增强字符串提取;切换到'append'方法来添加数据到data.frame(相对于预分配和插入)。

最新更新