我的代码中有什么问题?我不知道如何结合结果。 这个代码是交付数据,搜索运单很麻烦,所以我尝试了!但这很难。
这是代码,
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| ", "", 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
NO CP
1 1Z30A2920429463047 DELIVERED
NO CP
1 1Z30A2920422913297 DELIVERED
NO CP
1 1Z30A2920439995052 DELIVERED
NO CP
1 1Z30A2920423741926 DELIVERED
但我希望这个结果如下,
NO CP
1 1Z30A2920429127213 DELIVERED
2 1Z30A2920429463047 DELIVERED
3 1Z30A2920422913297 DELIVERED
4 1Z30A2920439995052 DELIVERED
5 1Z30A2920423741926 DELIVERED
谢谢。
答案 0 :(得分: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| ", "", 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
希望这有帮助!