【问题标题】:Scrape table rvest刮桌背心
【发布时间】:2019-11-18 04:34:21
【问题描述】:

我正在尝试使用rvest in R 从网站上抓取表格,但出现错误:

"open.connection(x, "rb") 中的错误:HTTP 错误 404"

我该如何解决这个问题?

我已经尝试了一些选项,但都没有奏效。我不知道我做错了什么。我想知道我是否以错误的方式编码。这似乎是一件很简单的事情,但我无法解决。

options(stringsAsFactors = FALSE)

url<- "https://www.tutiempo.net/clima/06-2018/ws-830950.html"

tbl.names <- data.frame(
  "Temperatura Media" = character(0),
  "Temperatura Máxima" = character(0),
  "Temperatura Mínima" = character(0),
  "SLP" = character(0),
  "H" = character(0),"PP" = character(0),
  "VV" = character(0),
  "V" = character(0),"VM" = character(0),
  "VG" = character(0),
  "RA" = character(0),"SN" = character(0),
  "TS" = character(0),
  "FG" = character(0)  )

for (i in 1:31) {
  url <- paste0(url, i)
  tbl.page <- url %>%
    read_html() %>%
    html_nodes(xpath='//*[@id="ColumnaIzquierda"]/div/div[4]') %>%
    html_table()
  names(tbl.page[[1]]) <- names(tbl.names)
  tbl.names <- bind_rows(tbl.names, tbl.page[[1]])
}

我希望取回网站上的同一张表,但在 .xls.csv 文件中,以便我可以操作它。 出现这个错误

“open.connection(x, "rb") 中的错误:HTTP 错误 404”。

虽然,我不知道编码是否正常。

【问题讨论】:

    标签: r web-scraping datatable rvest


    【解决方案1】:

    我会采用与其他答案类似的方法,但由于表可以由类唯一标识,并且类是第二快的 css 选择器方法,我将检索单个节点(表),并通过类匹配

    library(rvest)
    
    url ="https://www.tutiempo.net/clima/06-2018/ws-830950.html" 
    df = read_html(url) %>% html_node(".mensuales") %>% html_table(fill = TRUE) %>% as.data.frame()
    

    【讨论】:

      【解决方案2】:

      我认为您可以使用 rvesttidyverse 来做到这一点。

      第一件事是将您的链接读取为带有read_html()的网址

      library(tidyverse)
      library(rvest)
      
      url ="https://www.tutiempo.net/clima/06-2018/ws-830950.html" 
      
      
      data = read_html(url)
      

      然后你必须用你感兴趣的信息来识别节点。在你使用html_nodes("table")的情况下,你可以看到这个网站有5个HTML表格,有趣的信息在第4个节点:

      data %>%
         html_nodes("table")
      
      {xml_nodeset (5)}
      [1] <table cellpadding="0" cellspacing="0"><tbody><tr>\n<td class="home"><a href="https://www.tutiempo.net/">El tiempo</a></td>\n<td><a href=" ...
      [2] <table cellpadding="0" cellspacing="0"><tr>\n<td><a href="/clima/ws-830950.html">Clima</a></td>\n\t\t\t\t\t<td><a href="/clima/2018/ws-830 ...
      [3] <table cellpadding="0" cellspacing="0"><tr>\n<td><span class="social facebook iw-facebook" title="Compartir en Facebook"></span></td>\n<td ...
      [4] <table cellpadding="0" class="medias mensuales numspan" style="width:100%;" cellspacing="0">\n<tr>\n<th>Día</th>\n<th><abbr class="tooltip ...
      [5] <table cellpadding="0" cellspacing="0" class="info">\n<tr>\n<td>T</td>\n<td>Temperatura media (°C)</td>\n</tr>\n<tr>\n<td>TM</td>\n<td>Tem ...
      

      一旦确定,您只需检索该节点:

      tab_temp = data %>%
        html_nodes("table") %>%
        .[4] %>%
        html_table(fill = TRUE) %>%
        as.data.frame()
      
      glimpse(tab_temp)
      Observations: 32
      Variables: 15
      $ Día <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "…
      $ T   <chr> "26.1", "25.1", "26.7", "", "", "", "25.7", "24.7", "26.5", "", "", "", "27.2", "26.1", "26.6", "", "", "", "25.2", "26.6", "26.3"…
      $ TM  <chr> "28", "28", "29", "", "", "", "28", "28", "29", "", "", "", "29", "27", "29", "", "", "", "29", "29", "29", "", "", "", "29", "27"…
      $ Tm  <chr> "24", "23", "24", "", "", "", "23", "23", "23", "", "", "", "23", "23", "23", "", "", "", "23", "25", "23", "", "", "", "22", "20"…
      $ SLP <chr> "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "",…
      $ H   <chr> "74", "89", "82", "", "", "", "85", "90", "82", "", "", "", "68", "74", "70", "", "", "", "77", "68", "70", "", "", "", "68", "83"…
      $ PP  <chr> "-", "-", "-", "", "", "", "-", "-", "0", "", "", "", "0", "-", "-", "", "", "", "-", "0", "0", "", "", "", "-", "-", "-", "", "",…
      $ VV  <chr> "9.7", "8.9", "10", "", "", "", "9", "8.7", "10", "", "", "", "10", "10", "9.8", "", "", "", "9.7", "10", "10", "", "", "", "9.7",…
      $ V   <chr> "11.3", "9.1", "13.7", "", "", "", "8.5", "7.2", "10.6", "", "", "", "19.8", "15.9", "18.1", "", "", "", "16.7", "19.3", "18", "",…
      $ VM  <chr> "18.3", "33.5", "24.1", "", "", "", "22.2", "16.5", "18.3", "", "", "", "29.4", "31.7", "29.4", "", "", "", "29.4", "24.1", "25.9"…
      $ VG  <chr> "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "",…
      $ RA  <chr> "o", "o", "o", "o", "", "o", "o", "o", "", "", "", "", "", "o", "o", "o", "o", "o", "o", "", "", "o", "", "", "o", "o", "o", "", "…
      $ SN  <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Medias y …
      $ TS  <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Medias y …
      $ FG  <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Medias y …
      

      然后你可以将你的数据框tab_temp保存为.csv,如果你想使用write.csv()rio::export()

      【讨论】:

      • + 你的,因为我的只是有点不同
      • 非常感谢!那太棒了!我尝试使用 CUrl 库,它更容易、更简单,但正如您在输出中看到的那样,一些变量没有显示它们的值(例如:第 4 天、第 5 天、第 6 天、第 10 天的温度......等等)。似乎有些数据是不可见的,我无法将其删除。你知道有什么办法可以解决吗?谢谢你
      • 没有。我不知道为什么此信息不可见。
      猜你喜欢
      • 2021-08-05
      • 1970-01-01
      • 2019-07-22
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2015-02-25
      • 2020-02-29
      相关资源
      最近更新 更多