Как перебрать большое количество идентификаторов при извлечении данных с веб-сайта в R, используя rvest и stringr? - PullRequest
0 голосов
/ 14 мая 2019

У меня есть список идентификаторов скважин (wdid), и я хочу получить их соответствующие записи о переадресации с указанного веб-сайта.Основываясь на коде, который работает для аналогичного запроса на другом сайте, я придумал следующий код.Тем не менее, я получаю «0» или «список ()» только после того, как код «успешно» выполняется.Я не продвинутый пользователь R и веб-пересмотра также.Может кто-нибудь помочь мне с тем, что мне не хватает?Код выглядит следующим образом:

#Install Packages

install.packages(c("httr", "jsonlite", "lubridate"))
install.packages('stringr')

library(rvest)
library(stringr)
library(jsonlite)
library(lubridate)
options(stringsAsFactors = FALSE)

#Load External Data

Wells<-read.csv("~/file path/Wells.csv",header=TRUE)  #This is the list of the Well IDs whose diversion record I want to pull
  n=length(Wells[,1])

#Specify the URL
URL.1 <- "http://dnrweb.state.co.us/DWR/DwrApiService/api/v2/structures/divrec/divrecyear/?format=csvforced&wdid="

#Run Loop
for(i in 1:n){
  tryCatch({    #This is to keep the loop from breaking if there is an error
         wdid<-2005002 #This is the first Well ID in the list
        #Wells[i,1]

  url<-paste(URL.1,"0",wdid,sep="")

  site<-read_html(url)

  d<-html_nodes(site,"#tabs-4 table")%>%  #This finds the spot in the html I need
    html_table()                          #Because what I need is a table, this keeps in table form

  ifelse(length(d)!=0,dd<-data.frame(d,wdid),dd<-0) #The length test makes sure the record is not empty

  ifelse(i==1,output<-dd,output<-rbind(output,dd))

  Sys.sleep(0.5)
  },error=function(e){cat("ERROR :",conditionMessage(e), wdid, "\n")}) #Displays error that would have broken loop
}

temp<-output[output$wdid>0,]
output.2<-merge(Wells,temp,by.x="dataValue",by.y="wdid",all=TRUE)
write.csv(output.2,file="~/file path/records.csv")


Ниже приведен пример идентификаторов моих скважин:

wdid
2005002
2005003
2005004
2005007
2005009
2005011
2005014
2005018
2005020
2005021
2005022
2005023
2005027
2005028
2005031
2005032
2005033
2005034
2005035
2005036
2005037
2005038
2005041
2005043
2005046
2005049
2005050
2005052
2005053
2005054
2005055
2005057
2005058
2005059
2005060
2005063
2005066
2005067
2005070
2005073
2005074
2005075
2005076
2005080
2005084
2005087
2005092
2005094
2005095
2005096
`````````


Результат, который он возвращает, выглядит следующим образом:

выход [, 1] выход 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0 дд 0


...