【问题标题】:Error: Replacement has length zero R错误:替换的长度为零 R
【发布时间】:2015-09-21 01:24:49
【问题描述】:

我正在尝试抓取http://then.gasbuddy.com/

我正在 R 中运行下一个代码

 library(RCurl)
 library(XML)
 doc <- htmlTreeParse('http://www.southcarolinagasprices.com/GasPriceSearch.aspx?typ=adv&fuel=A&srch=0&area=All%20Areas&station=All%20Stations&tme_limit=4')
rootNode <- xmlRoot(doc)

((rootNode[[2]][4])[1][[1]])[[15]][[1]][[11]][[1]][[1]][[2]][[8]][[1]][[2]][[1]][[1]][[1]][[1]][[1]][[1]]

#<div class="p1"/>

x <- matrix(, nrow = 20, ncol = 4)

x[1,1] <- xmlValue(((rootNode[[2]][4])[1][[1]])[[15]][[1]][[11]][[1]][[1]][[2]][[8]][[1]][[2]][[1]][[1]][[1]][[1]][[1]][[1]])

但是我有这个错误

替换的长度为零

如何减去 p1 并将其放入矩阵中?

【问题讨论】:

    标签: r web-scraping rcurl webscarab


    【解决方案1】:

    您想出了一个有趣的方法来绕过他们的价格混淆。由于他们没有在服务条款中限制抓取,因此您可以通过以下一种方式来抓取价格:

    library(xml2)
    
    doc <- read_html('http://www.southcarolinagasprices.com/GasPriceSearch.aspx?typ=adv&fuel=A&srch=0&area=All%20Areas&station=All%20Stations&tme_limit=4')
    
    prices <- xml_find_all(doc, xpath="//div[@class='sp_p']")
    
    sapply(prices, function(x) {
      as.numeric(paste(gsub("d", "\\.", 
                            gsub("^p", "", 
                                 unlist(xml_attrs(xml_find_all(x, "./div"))))),
                       collapse=""))
    })
    
    ##   [1] 1.65 1.65 1.65 1.65 1.65 1.65 1.65 1.65 1.65 1.67 1.68 1.69 1.69 1.69 1.69 1.69 1.69 1.69 1.69
    ##  [20] 1.70 1.71 1.72 1.72 1.73 1.73 1.73 1.73 1.73 1.73 1.73 1.73 1.73 1.74 1.74 1.74 1.74 1.74 1.74
    ##  [39] 1.74 1.74 1.74 1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.75 1.76 1.76
    ##  [58] 1.76 1.76 1.76 1.76 1.76 1.76 1.76 1.76 1.76 1.76 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77
    ##  [77] 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77
    ##  [96] 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.77 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78
    ## [115] 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78
    ## [134] 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.78 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79
    ## [153] 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79
    ## [172] 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79
    ## [191] 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79 1.79
    

    【讨论】:

    • 它有效,谢谢,我是 R 的新手,你知道将这些值转换为 data.frame 的方法吗?
    • 你能再扩展一下吗?你只是想要天然气价格还是整个“表格”作为数据框?如果是后者,这对于一个新问题与扩大这个问题的范围很有好处
    • 我只需要 data.frame 中的 gas 价格将其导出为 .csv 或 .txt
    • 不,你可以用writeLines
    【解决方案2】:

    错误意味着它所说的。看看返回值来自

    xmlValue(((rootNode[[2]][4])[1][[1]])[[15]][[1]][[11]][[1]][[1]][[2]][[8]][[1]][[2]][[1]][[1]][[1]][[1]][[1]][[1]])
    

    这是

    character(0)
    

    因为&lt;div class="p1"/&gt; 是一个不包含任何文本的自闭合标签。如错误消息所示,在 R 中将向量的一部分替换为长度为零的内容是错误的。如果您希望这些长度为零的结果返回 NA"" 之类的内容,则需要使用 if/else 构造。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多