这是我的代码:
location_df <- data.frame(unique(location_html),stringsAsFactors = FALSE)
for (i in location_df) {
con <- url(i,"rb")
webpage <- read_html(con)
.
<operations performed here>
.
}
要提供有关location_df的更多信息,以下是str(location_df)的结果:
'data.frame': 659 obs. of 1 variable:
$ location_html: chr "https://www.faredetective.com/farehistory/flights-to-
Al_Ain-AAN.html" "https://www.faredetective.com/farehistory/flights-to-
Allentown-ABE.html" "https://www.faredetective.com/farehistory/flights-to-
Albuquerque-ABQ.html" "https://www.faredetective.com/farehistory/flights-to-Abuja-ABV.html" ...
我得到的错误是“ URL(i,“ rb”)中的错误:无效的'description'参数”
当我将其粘贴到location_df中的一个字符串中时,它可以工作,但是如果没有在第一次循环尝试中触发此错误,就无法使其成功遍历数据帧。我的代码有什么问题?
(我很抱歉在其他地方回答过此问题;我浏览了所有“相似的问题”,但找不到解决我问题的答案)
答案 0 :(得分:3)
您忘记在for循环中指定列。试试:
for (i in location_df$location_html) {
con <- url(i,"rb")
webpage <- read_html(con)
# .
# <operations performed here>
# .
}