2017-08-04 16 views
1

私のコードで何が問題ですか?私は結果を組み合わせる方法を知らない。 このコードはupsの配信データであり、私がそれを試したので、私はそれを試しました!しかし、それは困難でした。これは、コードでR反復コード、クロール結果

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) 
} 

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

はあなたに感謝し、このコードの結果が

   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 

あるしかし、私は次のように、この結果はなりたいです。

答えて

0

リストには、あなたの中間結果を追加し、次のようにそれらをrowbindできます

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 

結果:

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

は、この情報がお役に立てば幸いです!

+0

ああ私の良さ..おかげで天才! –

関連する問題