Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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/4/r/73.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
Xml 解析失败时的htmlParse循环重试值_Xml_R_Loops_Web Scraping - Fatal编程技术网

Xml 解析失败时的htmlParse循环重试值

Xml 解析失败时的htmlParse循环重试值,xml,r,loops,web-scraping,Xml,R,Loops,Web Scraping,如何在循环中重试我的值 我正在解析html,每隔一段时间,当我试图绑定另一个数据帧中的行时,解析将不会捕获整个表,从而导致代码中出现错误。我有另一个变量,它有我可以检查的实际行数 我的想法是 for(thisURL in URLs){ for (l in 1:10) { b <- htmlParse(thisURL) tableNode <- xpathSApply(batting, '//*[@id="logs"]')[[1]] data <-

如何在循环中重试我的值

我正在解析html,每隔一段时间,当我试图绑定另一个数据帧中的行时,解析将不会捕获整个表,从而导致代码中出现错误。我有另一个变量,它有我可以检查的实际行数

我的想法是

for(thisURL in URLs){

  for (l in 1:10) {
    b <- htmlParse(thisURL)
    tableNode <- xpathSApply(batting, '//*[@id="logs"]')[[1]]
    data <- readHTMLTable(tableNode, stringsAsFactors = FALSE)
    gid <- xpathSApply(b, '//*[contains(@id, "logs.")]/td[12]/span/@id')

    if length(data[[1]]) == length(gid) then exit this loop continue with the original loop else retry the htmlParse

  }

  remainder of first for loop

}
for(URL中的此URL){
对于(1:10中的l){

b我不知道索引
l
在1:10循环中做了什么。不过,你似乎想要一个
while
语句。可能是这样的吧

for(thisURL in URLs){

  # These assignments get the while loop started
  data <- NULL 
  gid <- NA

  # Number of retries and initial l
  l.max <- 10  
  l <- 0

  # This will run as long as the lengths are unequal for at most l.max times 
  while (length(data[[1]]) != length(gid) & l < l.max) {
    l <- l+1
    tableNode <- xpathSApply(batting, '//*[@id="logs"]')[[1]]
    data <- readHTMLTable(tableNode, stringsAsFactors = FALSE)
    gid <- xpathSApply(b, '//*[contains(@id, "logs.")]/td[12]/span/@id')
  }

  remainder of first for loop

}
for(URL中的此URL){
#这些赋值使while循环开始

数据什么是“此”循环和什么是“原始”循环?“此”循环是1:10循环和“原始”loop是URL循环中的thisURL如果您最终试图从URL列表的页面上提取的数据构建数据帧,有一种比两个
循环更好的方法。@hrbrmstr您需要我做什么才能做得更好?使用'dput(URL)`很抱歉,我没有看到它。它是进度条的一部分。如果我做一段时间,如果它相等,while不会停止吗?如果两者相等,你不希望循环停止吗?我建议的代码会启动1:10循环,但如果数据和网格长度相同,它会在10之前停止。我认为我的文章应该可以工作你用过了。我以前为了重试10次而循环。我可能也向后解释过,因为我希望代码在它们相等时执行,所以while(length(data[[1]])==length(gid))可能是这样。我希望发生的是,如果它们不相等,它只会重试当前url,直到do相等并继续。这更有意义吗?我编辑了我的答案,希望它更清晰。while循环将开始并运行,直到长度相等。然后它停止,url循环的其余部分停止将继续。如果您知道最终这两个部分的长度将相等,则不需要l.max部分。如果这两个部分可能永远不相等(在这种情况下,while循环将永远不会终止),则需要此部分。