在R中的HTTP/FTP服务器上列出文件 [英] List files on HTTP/FTP server in R

查看:104
本文介绍了在R中的HTTP/FTP服务器上列出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从R!获取HTTP/FTP服务器上的文件列表,以便在下一步中我可以下载它们(或选择一些符合我的标准的文件进行下载).

I'm trying to get list of files on HTTP/FTP server from R!, so that in next step I will be able to download them (or select some of files which meet my criteria to download).

我知道可以在Web浏览器(下载管理器)中使用外部程序,这将允许我选择要从当前Web页面/FTP下载的文件.但是,我希望将所有内容都编写成脚本,以便我更轻松地进行复制.

I know that it is possible to use external program in web browser (download manager) which will allow me to select files to download from current web page/ftp. However, I wish to have everything scripted, so that it will be easier for me to reproduce.

我考虑过从R调用Python! (因为它似乎容易得多),但我尝试完全在R中做到这一点!

I thought about calling Python from R! (since it seems much easier), but I tried to do that entirely in R!

我写了以下几行

require("RCurl") 
result <- getURL("http://server",verbose=TRUE,ftp.use.epsv=TRUE, dirlistonly = TRUE)

结果变量是字符类型:

typeof(result)
[1] "character"

示例内容如下:

有趣的文件_20150629.txt2015年8月20日09:31 289K \ n有趣 file_20150630.txt2015年8月20日09:31 293K \ n有趣 file_20150701.txt2015年8月20日09:31 301K \ n有趣 file_20150702.txt2015年8月20日09:31 304K \ n有趣 file_20150703.txt2015年8月20日09:31 301K \ n有趣 file_20150704.txt2015年8月20日09:31 300K \ n有趣 file_20150705.txt2015年8月20日09:31 300K \ n有趣 file_20150706.txt2015年8月20日09:31 305K \ n有趣 file_20150707.txt2015年8月20日09:31 305K \ n有趣 file_20150708.txt2015年8月20日09:31 301K \ n有趣 file_20150709.txt2015年8月20日09:31 294K \ n


\ n \ n \ n"

Interesting file_20150629.txt20 Aug-2015 09:31 289K\nInteresting file_20150630.txt20 Aug-2015 09:31 293K\nInteresting file_20150701.txt20 Aug-2015 09:31 301K\nInteresting file_20150702.txt20 Aug-2015 09:31 304K\nInteresting file_20150703.txt20 Aug-2015 09:31 301K\nInteresting file_20150704.txt20 Aug-2015 09:31 300K\nInteresting file_20150705.txt20 Aug-2015 09:31 300K\nInteresting file_20150706.txt20 Aug-2015 09:31 305K\nInteresting file_20150707.txt20 Aug-2015 09:31 305K\nInteresting file_20150708.txt20 Aug-2015 09:31 301K\nInteresting file_20150709.txt20 Aug-2015 09:31 294K\n


\n\n\n"


所以现在,我正在尝试解析结果内容:


So now, I'm trying to parse result content:

myFiles <- strsplit(result,'<a[^>]* href=\\"([^"]*.txt)\\"')[[1]]

希望我会匹配txt文件(因为它在方括号()中).但它匹配:

hoping that I will match txt file (since it's in brackets: ()). but it matches:

">Interesting file_20150706.txt</a></td><td align=\"right\">20 Aug-2015 09:31  </td><td align=\"right\">305K</td></tr>\n<tr><td valign=\"top\"><img src=\"/apacheIcons/text.gif\" alt=\"[TXT]\"></td><td>

相反.

出了什么问题(我在 https://regex101.com/上测试了我的表情)或(也许是这个问题更为合适),在R中,有一种更简单的方法来获取服务器上具有特定扩展名的文件列表! ?

What is wrong (I tested my expression on https://regex101.com/) or (maybe this question is more appropriate) there is much easier way to obtain list of files with specific extension on the server in R! ?

推荐答案

您真的不应该在html上使用正则表达式. XML软件包使这一过程变得非常简单.我们可以使用getHTMLLinks()来收集我们想要的任何链接.

You really shouldn't use regex on html. The XML package makes this pretty simple. We can use getHTMLLinks() to gather any links we want.

library(XML)
getHTMLLinks(result)
#  [1] "Interesting file_20150629.txt"   "Interesting file_20150630.txt"  
#  [3] "Interesting file_20150701.txt"   "Interesting file_20150702.txt"  
#  [5] "Interesting file_20150703.txt"   "Interesting file_20150704.txt"  
#  [7] "Interesting file_20150705.txt"   "Interesting file_20150706.txt"  
#  [9] "Interesting file_20150707.txt"   "Interesting file_20150708.txt"  
# [11] "Interesting file_20150709.txt"  

这将获取//a中包含的所有/@href链接.要仅捕获包含 .txt的查询,可以使用与默认查询不同的XPath查询.

That will get all /@href links contained in //a. To grab only the ones that contain .txt, you can use a different XPath query from the default.

getHTMLLinks(result, xpQuery = "//a/@href[contains(., '.txt')]")

或更准确地说,要获得以.txt结尾结束的文件,您可以

Or even more precisely, to get those files that end with .txt, you can do

getHTMLLinks(
    result,
    xpQuery = "//a/@href['.txt'=substring(., string-length(.) - 3)]"
) 

这篇关于在R中的HTTP/FTP服务器上列出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆