Попробуйте другую стратегию.
##########################
####
#### Scrape http://plants.jstor.org/specimen/
#### Idea:: Gather links from http://plants.jstor.org/search?t=2076
#### Then follow links:
####
#########################
library(RCurl)
library(XML)
### get search page::
cookie = 'cookiefile.txt'
curl = getCurlHandle ( cookiefile = cookie ,
useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en - US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6",
header = F,
verbose = TRUE,
netrc = TRUE,
maxredirs = as.integer(20),
followlocation = TRUE)
querry.jstor <- getURL('http://plants.jstor.org/search?t=2076', curl = curl)
## remove white spaces:
querry.jstor2 <- gsub('\r','', gsub('\t','', gsub('\n','', querry.jstor)))
### get links from search page
getLinks = function() {
links = character()
list(a = function(node, ...) {
links <<- c(links, xmlGetAttr(node, "href"))
node
},
links = function()links)
}
## retrieve links
querry.jstor.xml.parsed <- htmlTreeParse(querry.jstor2, useInt=T, handlers = h1)
## cleanup links to keep only the one we want.
querry.jstor.links = NULL
querry.jstor.links <- c(querry.jstor.links, querry.jstor.xml.parsed$links()[-grep('http', querry.jstor.xml.parsed$links())]) ## remove all links starting with http
querry.jstor.links <- querry.jstor.links[-grep('search', querry.jstor.links)] ## remove all search links
querry.jstor.links <- querry.jstor.links[-grep('#', querry.jstor.links)] ## remove all # links
querry.jstor.links <- querry.jstor.links[-grep('javascript', querry.jstor.links)] ## remove all javascript links
querry.jstor.links <- querry.jstor.links[-grep('action', querry.jstor.links)] ## remove all action links
querry.jstor.links <- querry.jstor.links[-grep('page', querry.jstor.links)] ## remove all page links
## number of results
jstor.article <- getNodeSet(htmlTreeParse(querry.jstor2, useInt=T), "//article")
NumOfRes <- strsplit(gsub(',', '', gsub(' ', '' ,xmlValue(jstor.article[[1]][[1]]))), split='')[[1]]
NumOfRes <- as.numeric(paste(NumOfRes[1:min(grep('R', NumOfRes))-1], collapse = ''))
for(i in 2:ceiling(NumOfRes/20)){
querry.jstor <- getURL('http://plants.jstor.org/search?t=2076&p=',i, curl = curl)
## remove white spaces:
querry.jstor2 <- gsub('\r','', gsub('\t','', gsub('\n','', querry.jstor)))
querry.jstor.xml.parsed <- htmlTreeParse(querry.jstor2, useInt=T, handlers = h1)
querry.jstor.links <- c(querry.jstor.links, querry.jstor.xml.parsed$links()[-grep('http', querry.jstor.xml.parsed$links())]) ## remove all links starting with http
querry.jstor.links <- querry.jstor.links[-grep('search', querry.jstor.links)] ## remove all search links
querry.jstor.links <- querry.jstor.links[-grep('#', querry.jstor.links)] ## remove all # links
querry.jstor.links <- querry.jstor.links[-grep('javascript', querry.jstor.links)] ## remove all javascript links
querry.jstor.links <- querry.jstor.links[-grep('action', querry.jstor.links)] ## remove all action links
querry.jstor.links <- querry.jstor.links[-grep('page', querry.jstor.links)] ## remove all page links
Sys.sleep(abs(rnorm(1, mean=3.0, sd=0.5)))
}
## make directory for saving data:
dir.create('./jstorQuery/')
## Now we have all the links, so we can retrieve all the info
for(j in 1:length(querry.jstor.links)){
if(nchar(querry.jstor.links[j]) != 1){
querry.jstor <- getURL('http://plants.jstor.org',querry.jstor.links[j], curl = curl)
## remove white spaces:
querry.jstor2 <- gsub('\r','', gsub('\t','', gsub('\n','', querry.jstor)))
## contruct name:
filename = querry.jstor.links[j][grep( '/', querry.jstor.links[j])+1 : nchar( querry.jstor.links[j])]
## save in directory:
write(querry.jstor2, file = paste('./jstorQuery/', filename, '.html', sep = '' ))
Sys.sleep(abs(rnorm(1, mean=3.0, sd=0.5)))
}
}