Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用rvest在Rstudio中运行循环时返回多个错误索引和http错误_R_Loops_Web Scraping_Rvest - Fatal编程技术网

使用rvest在Rstudio中运行循环时返回多个错误索引和http错误

使用rvest在Rstudio中运行循环时返回多个错误索引和http错误,r,loops,web-scraping,rvest,R,Loops,Web Scraping,Rvest,我试图计算每个国家之间的飞行时间,希望为我正在研究的欺诈预防工具创建参数 我正在使用的网站url是 我的第三栏是用所有可能的组合替换两个国家的参考 我正在尝试使用RVEST和循环来实现这一点,但不断收到各种错误。我曾在stack上寻找其他解决方案来解决我的问题,但遇到了许多问题。最后,我试图创建一个循环,在一个短窗口内,我查询的网站不会被55225个请求破坏 这是我尝试过的最新解决方案,但我在重复中不断出现以下错误 我试着重新安排我的数据帧,并处理起点和终点的替换 我试过用硒来做这件事,但也遇到

我试图计算每个国家之间的飞行时间,希望为我正在研究的欺诈预防工具创建参数

我正在使用的网站url是

我的第三栏是用所有可能的组合替换两个国家的参考

我正在尝试使用RVEST和循环来实现这一点,但不断收到各种错误。我曾在stack上寻找其他解决方案来解决我的问题,但遇到了许多问题。最后,我试图创建一个循环,在一个短窗口内,我查询的网站不会被55225个请求破坏

这是我尝试过的最新解决方案,但我在重复中不断出现以下错误

我试着重新安排我的数据帧,并处理起点和终点的替换

我试过用硒来做这件事,但也遇到了其他问题

我尝试重新格式化其他类似问题的解决方案,但仍然收到错误

tables <- list()
index <- 1
for (i in CountryPairs){
    try(
        {
            url <- paste0("https://www.travelmath.com/flying-time/from/",i)
            table <- url %>%
            read_html()%>%
            html_nodes("#flyingtime")

            tables[index] <- table

            index <- index +1
        }
    )
}
df<-do.call("rbind",tables)

tables我列出了一个国家列表来构建您的
CountryPairs
变量,并使用您的代码得出了这个结果。
表格
变量以飞行时间作为字符向量填充。由于出现了一些HTTP 400错误,我认为问题在于生成
CountryPairs
变量的方式,从而创建了一个错误的请求

library(dplyr)
library(rvest)

# Vector of countries
countries <- c(
  "Afghanistan",
  "Albania",
  "Algeria",
  "Andorra",
  "Angola",
  "Argentina",
  "Armenia",
  "Australia",
  "Austria",
  "Azerbaijan"
)

# Build all combinations of two countries
countries_combinations <- combn(countries, 2)

# Build the country pairs as "Country1/to/Country2" for the request to travelmath
country_pairs <- apply(countries_combinations, 2, function(x) paste(x, collapse = "/to/"))

tables <- list()
index <- 1
for (c_pair in country_pairs){
  try(
    {
      url <- paste0("https://www.travelmath.com/flying-time/from/", c_pair)

      # Get the flight time from the #flyingtime h3 tag
      table <- url %>%
        read_html %>%
        html_nodes("#flyingtime") %>%
        html_text

      tables[index] <- table

      index <- index + 1
    }
  )
}
库(dplyr)
图书馆(rvest)
#国家矢量

HTTP错误400表示请求不正确。请提供CountryPairs的样本,以便我们测试您的代码?是否尝试打印
url
变量以查看其是否正确?是否会在帮助结束时关闭所有连接,并出现以下错误?In.Internal(gc(详细、重置、完全)):关闭未使用的连接3
CatchupPause <- function(secs){
 Sys.sleep(secs) # pause to let connection work
 closeAllConnections()
 gc()
}