我想在R中获取一个.url
快捷文件(用Windows制作)的网址。
文件格式如下所示:
[{000214A0-0000-0000-C000-000000000046}]
Prop4=31,Stack Overflow - Where Developers Learn, Share, & Build Careers
Prop3=19,11
[{A7AF692E-098D-4C08-A225-D433CA835ED0}]
Prop5=3,0
Prop9=19,0
[InternetShortcut]
URL=https://stackoverflow.com/
IDList=
IconFile=https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d
IconIndex=1
[{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}]
Prop5=8,Microsoft.Website.E7533471.CBCA5933
而且有一些文件。
我用过file.info()
。但我猜,它只显示了第一个属性头的信息。
我需要在R中这样做,因为我有一个很长的.url
文件列表,这是我需要转换的地址。
发布于 2017-11-12 19:53:33
粗野的方式(我一会儿会更新这个):
ini::read.ini("https://rud.is/dl/example.url")$InternetShortcut$URL
## [1] "https://rud.is/b/2017/11/11/measuring-monitoring-internet-speed-with-r/"
稍微不那么粗糙:
read_url_shortcut <- function(x) {
require(ini)
x <- ini::read.ini(x)
x[["InternetShortcut"]][["URL"]]
}
没有ini
包依赖关系:
read_url_shortcut <- function(x) {
x <- readLines(x)
x <- grep("^URL", x, value=TRUE)
gsub("^URL[[:space:]]*=[[:space:]]*", "", x)
}
更多的“生产价值”版本:
#' Read in internet shortcuts (.url or .webloc) and extract URL target
#'
#' @param shortcuts character vector of file path+names or web addresses
#' to .url or .webloc files to have URL fields extracted from.
#' @return character vector of URLs
read_shortcut <- function(shortcuts) {
require(ini)
require(xml2)
require(purrr)
purrr::map_chr(shortcuts, ~{
if (!grepl("^http[s]://", .x)) {
.x <- path.expand(.x)
if (!file.exists(.x)) return(NA_character_)
}
if (grepl("\\.url$", .x)) {
.ini <- suppressWarnings(ini::read.ini(.x)) # get encoding issues otherwise
.ini[["InternetShortcut"]][["URL"]][1] # some evidence multiple are supported but not sure so being safe
} else if (grepl("\\.webloc$", .x)) {
.x <- xml2::read_xml(.x)
xml2::xml_text(xml2::xml_find_first(.x, ".//dict/key[contains(., 'URL')]/../string"))[1] # some evidence multiple are supported but not sure so being safe
} else {
NA_character_
}
})
}
理想情况下,这样的函数将返回一个数据帧行,其中包含可以找到的所有相关信息(标题、URL和图标URL、创建/mod日期等)。我不希望我的Windows保持足够长的时间来生成足够多的示例来完成这个任务。
注意:“生产”-ready版本仍然不能很好地处理文件或web地址不可读/不可及的边缘情况,也不处理格式错误的.url
或.webloc
文件。
https://stackoverflow.com/questions/47252960
复制相似问题