无法使用R中的Webshot()包将多个URL转换为png图像

时间:2018-10-13 21:36:16

标签: r

我是R编程的初学者,但是我研究了与Webshot()软件包有关的所有可用细节。当我为单个URL运行时,下面的代码有效。

library(webshot)
webshot::webshot(url = "C:/Users/bazinga/Documents/test/abc.html",
                 file = "C:/Users/bazinga/Documents/test/abc.png", 
                 vwidth = 1000, vheight = 1000)

我正在尝试截图多个URL,并将其另存为png在一个文件夹中。 (参考:https://github.com/wch/webshot/blob/9a53e9e02e3936ccb32a3b767cfae6724a04026d/R/webshot.R#L63

savepath <-"C:/Users/bazinga/Documents/test/"      #Test Path
f <- list.files(path = savepath , pattern = ".html", recursive = TRUE)
f
# [1] "abc.html"  "def.html"     "ghi.html"    "jkl.html"    "mno.html"
x <- data.frame("SN" = 1:length(f), "ImageName" = tools::file_path_sans_ext(f) 
                ,"File" = c(f), "CreateDate" = c(file.info(glue("{savepath}{f}"))$ctime)
                , "InPath" = glue("{savepath}{f}")
                , "OutPath" = glue("{savepath}{tools::file_path_sans_ext(f)}.png")
)
x
# SN ImageName     File               CreateDate                                   InPath                                 OutPath
# 1       abc abc.html       2018-10-12 11:07:11 C:/Users/bazinga/Documents/test/abc.html C:/Users/bazinga/Documents/test/abc.png
# 2       def def.html       2018-10-12 11:07:11 C:/Users/bazinga/Documents/test/def.html C:/Users/bazinga/Documents/test/def.png
# 3       ghi ghi.html       2018-10-12 11:07:11 C:/Users/bazinga/Documents/test/ghi.html C:/Users/bazinga/Documents/test/ghi.png
# 4       jkl jkl.html       2018-10-12 11:07:11 C:/Users/bazinga/Documents/test/jkl.html C:/Users/bazinga/Documents/test/jkl.png
# 5       mno mno.html       2018-10-12 11:07:11 C:/Users/bazinga/Documents/test/mno.html C:/Users/bazinga/Documents/test/mno.png

将详细信息保存到数据框中并按如下所示运行webshot()代码

library(webshot)
webshot::webshot(url = c(x["InPath"]),
                 file = c(x["OutPath"]), 
                 vwidth = 1000, vheight = 1000)
  

path.expand(path)错误:无效的'path'参数

然后我尝试在数据框的Inpath和Outpath列的值中添加“”。

savepath <-"C:/Users/bazinga/Documents/test/"      #Test Path
    f <- list.files(path = savepath , pattern = ".html", recursive = TRUE)
    f
    # [1] "abc.html"  "def.html"     "ghi.html"    "jkl.html"    "mno.html"
    x <- data.frame("SN" = 1:length(f), "ImageName" = tools::file_path_sans_ext(f) 
                    ,"File" = c(f), "CreateDate" = c(file.info(glue("{savepath}{f}"))$ctime)
            , "InPath" = glue("\"{savepath}{f}\"")
            , "OutPath" = glue("\"{savepath}{tools::file_path_sans_ext(f)}.png\""){tools::file_path_sans_ext(f)}.png")
    )
    x
    # SN ImageName     File               CreateDate                                   InPath                                 OutPath
    # 1       abc abc.html       2018-10-12 11:07:11 "C:/Users/bazinga/Documents/test/abc.html" "C:/Users/bazinga/Documents/test/abc.png"
    # 2       def def.html       2018-10-12 11:07:11 "C:/Users/bazinga/Documents/test/def.html" "C:/Users/bazinga/Documents/test/def.png"
    # 3       ghi ghi.html       2018-10-12 11:07:11 "C:/Users/bazinga/Documents/test/ghi.html" "C:/Users/bazinga/Documents/test/ghi.png"
    # 4       jkl jkl.html       2018-10-12 11:07:11 "C:/Users/bazinga/Documents/test/jkl.html" "C:/Users/bazinga/Documents/test/jkl.png"
    # 5       mno mno.html       2018-10-12 11:07:11 "C:/Users/bazinga/Documents/test/mno.html" "C:/Users/bazinga/Documents/test/mno.png"

library(webshot)
webshot::webshot(url = c(x["InPath"]),
                 file = c(x["OutPath"]), 
                 vwidth = 1000, vheight = 1000)
  

vapply中的错误(URL,fix_one,character(1),USE.NAMES = FALSE):值必须为长度1,但FUN(X [[1]])结果为长度5 < / em>

我知道我可以多次调用webshot()并获得结果,但是请让我知道我尝试使用webshot()甚至是否可行,以及您是否有任何建议或将我指向右边仅调用一个命令即可解决此问题的方法。

0 个答案:

没有答案