Html 列出网页中的数据URL
为了自动下载网页中显示的所有文件(数据url与网页url不同),我必须从html代码中提取这些数据url。 我是这样做的:Html 列出网页中的数据URL,html,r,url,download,Html,R,Url,Download,为了自动下载网页中显示的所有文件(数据url与网页url不同),我必须从html代码中提取这些数据url。 我是这样做的: library(XML) url <- "http://www.data.gouv.fr/fr/dataset/registre-parcellaire-graphique-2012-contours-des-ilots-culturaux-et-leur-groupe-de-cultures-majorita" doc <- htmlParse(url) 有
library(XML)
url <- "http://www.data.gouv.fr/fr/dataset/registre-parcellaire-graphique-2012-contours-des-ilots-culturaux-et-leur-groupe-de-cultures-majorita"
doc <- htmlParse(url)
有什么想法吗?试试看。它返回URL页面上的所有链接。这个答案几乎是从
htmlpasse
帮助文件中逐字逐句地获得的。getLinks
函数非常有用
> library(XML)
> getLinks <- function() {
links = character()
list(a = function(node, ...) {
links <<- c(links, xmlGetAttr(node, "href"))
node
},
links = function()links)
}
> h1 <- getLinks()
> htmlTreeParse(url, handlers = h1)
> h1$links()
正如Richard提到的,帮助页面提供了许多示例作为演示。如果我们知道当前链接的模式,例如我们的例子
,您是否会碰巧知道为什么有时我会出现错误:无法加载HTML资源。
而有时我没有?我觉得这可能是内存问题,我可能需要free()
html文档,但我没有保存任何文档。谢谢,回答得很好!我被困在xpath步骤,尽管我有一些接近的东西!Richard,我被同样的问题难住了,很不情愿地不得不使用手动保存的html文件。我认为这是内存和连接超时的组合。我面临的另一个错误是函数中的getURL(url)错误(type,msg,asError=TRUE):无法连接到主机
> library(XML)
> getLinks <- function() {
links = character()
list(a = function(node, ...) {
links <<- c(links, xmlGetAttr(node, "href"))
node
},
links = function()links)
}
> h1 <- getLinks()
> htmlTreeParse(url, handlers = h1)
> h1$links()
h1$links()[grepl("data", h1$links())]
library(XML)
url <- "http://www.data.gouv.fr/fr/dataset/registre-parcellaire-graphique-2012-contours-des-ilots-culturaux-et-leur-groupe-de-cultures-majorita"
#Save and read html file,replace the filname here
doc <- htmlTreeParse('doc_fr.htm',useInternalNodes = TRUE)
#The pattern required for subsetting links
doc_nodes=xpathSApply(doc,"//div[@class='list-group-item']",xmlAttrs)
row.names(doc_nodes)
#[1] "class" "data-url" "data-format" "rel" "data-trigger" "data-placement"
#[7] "title" "data-content"
#The links are present in second row from above
doc_links=doc_nodes[2,]
head(doc_links,5)
#[1] "https://www.data.gouv.fr/storage/f/2014-02-12T09-44-05/Description_RPG_2012_SHP.rtfd.zip"
#[2] "https://www.data.gouv.fr/storage/f/2014-02-12T09-50-50/RPG_2012_%20Codes_groupes_cultures_et_couleurs.csv"
#[3] "https://www.data.gouv.fr/storage/f/2014-02-12T09-57-24/RPG_2012_001.zip"
#[4] "https://www.data.gouv.fr/storage/f/2014-02-12T13-26-51/RPG_2012_002.zip"
#[5] "https://www.data.gouv.fr/storage/f/2014-02-12T13-28-03/RPG_2012_02A.zip"
#Get file names from links
unique(do.call(rbind,lapply(strsplit(doc_links,split="/"),function(x) length(x))))
# [,1]
#[1,] 7
#replace space with underscore in output file name
raw_fnames=do.call(rbind,lapply(strsplit(doc_links,split="/"),function(x) x[7] ))
raw_fnames=gsub('%20',' ',raw_fnames)
new_fnames=gsub(' ','_',raw_fnames)
#Download all files
lapply(1:length(new_fnames),function(x) download.file(doc_links[x],destfile=new_fnames[x]) )
#trying URL 'https://www.data.gouv.fr/storage/f/2014-02-12T09-44-05/Description_RPG_2012_SHP.rtfd.zip'
#Content type 'application/zip' length 128101 bytes (125 Kb)
#opened URL
#downloaded 125 Kb