pachadotdev
pachadotdev

Reputation: 3775

R: rvest not capturing table

I'm trying to extract ~3000 tables from a website and putting it in one file

1st try:

library(rvest)
library(dplyr)
library(data.table)
library(readr)

  url = read_html("http://seia.sea.gob.cl/busqueda/buscarProyectoAction.php?_paginador_refresh=1&_paginador_fila_actual=1")

  relevant_table = url %>%
    html_nodes("table") %>% 
    html_table()

  relevant_table = url %>%
    html_nodes(xpath = '//*[contains(concat( " ", @class, " " ), concat( " ", "contenido", " " ))] | //td | //th') %>% 
    html_table()

It doesn't work so I did:

write_lines(url,"url.txt")

And I can see the <table> ... </table> part is not saved.

I read these links: Link1, Link2 and Link3

The xpath part comes after I tried inspecting the website with selector gadget from Chrome extensions.

2nd try:

library(XML)
library(httr)

url = "http://seia.sea.gob.cl/busqueda/buscarProyectoAction.php?_paginador_refresh=1&_paginador_fila_actual=1"
doc = htmlParse(GET(theurl, user_agent("Mozilla")))
results = xpathSApply(doc, '//*[contains(concat( " ", @class, " " ), concat( " ", "contenido", " " ))] | //td | //th')
results = readHTMLTable(results[[1]])

Same problem as 1st try. What I can notice is that the imported HTML does not cointain the table I can see when I view the source in Chrome.

If I go to view-source:http://seia.sea.gob.cl/busqueda/buscarProyectoAction.php?_paginador_refresh=1&_paginador_fila_actual=1 I can see the table:

<tr>
         <td>1</td>
         <td><a target=_new href='http://seia.sea.gob.cl/expediente/expediente.php?id_expediente=2132451239&modo=ficha' title='Proyecto Inmobiliario Hacienda Estancilla. Comuna de Valdivia. Región de los Ríos'>Proyecto Inmobiliario Hacienda Estancilla. Comuna de Valdivia. Región de los Ríos</a></td>
         <td>DIA</td>
         <td>Decimocuarta</td>
         <td align=center>h1</td>
         <td><span title="Teléfono: 222 333 232"> <a href="mailto:[email protected]">Daniel Andrés Suazo Quinteros</a></span></td>
         <td align=right>20,0000</td>
         <td align=right>02/06/2017</td>
         <td>En Admisión</td>
</tr>

Any ideas? many thanks in advance !!

Upvotes: 0

Views: 187

Answers (1)

vcaquilpan
vcaquilpan

Reputation: 86

I tried to scrape this page some months ago. I detected if you modify part of the url you can access to the table. You should change _paginador_refresh=1 to _paginador_refresh=0. I show you an example:

#Load libraries
library(rvest)
library(stringr)
library(dplyr)
library(stringr)

# base url
base_url <- "https://seia.sea.gob.cl/busqueda/buscarProyectoAction.php?nombre=&_paginador_refresh=0&_paginador_fila_actual="

# create an empty dataframe
final_table <- data.frame()

# Create a loop to query each page. Here we can scrape only first 10 pages
for (page in 1:10) {
  query <- read_html(str_c(base_url,page)) %>% 
    html_element(css = ".tabla_datos") %>% 
    html_table()
  final_table <- rbind(final_table,query) 
}

Upvotes: 1

Related Questions