2017-08-04 6 views
1

Was ist das Problem in meinem Code? Ich weiß nicht, wie man die Ergebnisse kombiniert. Dieser Code ist ups Lieferdaten, und es war mühsam, nach dem Frachtbrief zu suchen, also habe ich es versucht! Aber es war schwierig.R Wiederholungscode, Crawling-Ergebnis

Dieser Code ist,

library(stringr) 
Houseno <- c("1Z30A2920429127213","1Z30A2920429463047","1Z30A2920422913297","1Z30A2920439995052","1Z30A2920423741926") 
Houseno 
for (i in Houseno) 
{ 
url <- (paste0("https://iship.com/trackit/track.aspx?Track=",i)) 
line <- readLines(url, encoding = "UTF-8") 

#number 
upshouse <- line[which(str_detect(line,"UPS Tracking Number:"))] 
upshouse <- gsub("UPS Tracking Number:|<.+?>|\t|&nbsp;", "", upshouse) 

#result 
upsresult <- line[which(str_detect(line,"Status:"))] 
upsresult <- gsub("Status:|<.+?>|\t", "", upsresult) 

#com 
com <- data.frame(NO=upshouse, CP=upsresult) 
print(com) 
} 

Dieses Ergebnis Code ist

   NO  CP 
1 1Z30A2920429127213 DELIVERED 
       NO  CP 
1 1Z30A2920429463047 DELIVERED 
       NO  CP 
1 1Z30A2920422913297 DELIVERED 
       NO  CP 
1 1Z30A2920439995052 DELIVERED 
       NO  CP 
1 1Z30A2920423741926 DELIVERED 

Aber ich möchte dieses Ergebnis wie folgt,

  NO   CP 
1 1Z30A2920429127213 DELIVERED 
2 1Z30A2920429463047 DELIVERED 
3 1Z30A2920422913297 DELIVERED 
4 1Z30A2920439995052 DELIVERED 
5 1Z30A2920423741926 DELIVERED 

Ihnen danken.

Antwort

0

Sie könnten Ihre Zwischenergebnisse zu einer Liste hinzufügen und rowbind sie wie folgt:

library(stringr) 
Houseno <- c("1Z30A2920429127213","1Z30A2920429463047","1Z30A2920422913297","1Z30A2920439995052","1Z30A2920423741926") 

result <- vector('list',length(Houseno)) #initialize list with correct length 
for (i in 1:length(Houseno)) 
{ 
    url <- (paste0("https://iship.com/trackit/track.aspx?Track=",Houseno[i])) 
    line <- readLines(url, encoding = "UTF-8") 

    #number 
    upshouse <- line[which(str_detect(line,"UPS Tracking Number:"))] 
    upshouse <- gsub("UPS Tracking Number:|<.+?>|\t|&nbsp;", "", upshouse) 

    #result 
    upsresult <- line[which(str_detect(line,"Status:"))] 
    upsresult <- gsub("Status:|<.+?>|\t", "", upsresult) 

    #com 
    com <- data.frame(NO=upshouse, CP=upsresult) 
    result[[i]] <- com # add result to list 

} 

do.call(rbind,result) #rowbind the list to a single dataframe 

Ergebnis:

    NO  CP 
1 1Z30A2920429127213 DELIVERED 
2 1Z30A2920429463047 DELIVERED 
3 1Z30A2920422913297 DELIVERED 
4 1Z30A2920439995052 DELIVERED 
5 1Z30A2920423741926 DELIVERED 

hoffe, das hilft!

+0

Oh mein Gott .. Danke Genie !! –