2016-03-28 71 views
2

我想從公共網站獲取一些信息以執行研究。我希望獲取信息的網站是:https://declaraciones.sri.gob.ec/mat-vehicular-internet/reportes/general/valoresAPagar.jsp。在這個網站你必須把一個字符串,以獲得一些數據。問題是你需要雙擊一個按鈕才能顯示de信息。例如,通過使用字符串pyk0911我的下一個屏幕: enter image description here從R動態諮詢頁面的網頁抓取

然後,我要點擊「Buscar」我得到下一個屏幕: enter image description here 這個屏幕後,我一定要點擊「 Ver Avalos「,我將得到這個屏幕: enter image description here 這個最後的屏幕是我想要提取的對象,並保存在一個數據框或列表中。我想獲得這些信息的原因是因爲我有很多字符串,點擊和複製結果太長了。障礙是兩次雙擊。我想在R中構建一個函數來插入字符串,並從最終屏幕獲取所有信息,例如,Modelo,Año以及變量PeriodoAvaluo

回答

0

這裏有幾個步驟。首先,填寫表格並提交,然後提取到表格的鏈接,然後閱讀表格。

library("rvest") 
library("stringr") 

url <- "https://declaraciones.sri.gob.ec/mat-vehicular-internet/reportes/general/valoresAPagar.jsp" 

s <- html_session(url) 
s_form <- html_form(s)[[2]] 
filled_form <- set_values(s_form, placaCamv="pyk0911") 
out <- submit_form(session=s, filled_form) 

# out contains the link to the data table that pops up. This extracts that link 
dat_path <- out %>% html_nodes("input.boton") %>% html_attr("onclick") %>% 
    .[2] %>% str_extract("(?<=\\(\\').+(?=','avaluos)") 

# then read the second table. I assume this is what you need. 
df <- read_html(paste0("https://declaraciones.sri.gob.ec", dat_path)) %>% 
    html_table(fill=TRUE) %>% .[[2]] 
> df 
    Período Avalúo Impuesto 
1  2016 1,699.00  8.50 
2  2015 1,699.00  8.50 
3  2014 1,699.00  8.50 
4  2013 1,699.00  8.50 
5  2012 1,699.00  8.50 
6  2011 1,699.00  8.50 
7  2010 1,699.00  8.50 
8  2009 1,699.00  8.50 
9  2008 1,699.00  8.50 
10  2007 1,699.00  8.50 
11  2006 3,398.00 16.99 
12  2005 7,036.00 50.36 
13  2004 10,554.00 111.08 
14  2003 14,072.00 202.16 
15  2002 16,990.00 300.00 
16  2001 4,000.00 68.00 
+0

謝謝@cory工作完美! – Duck

0

如果您在單擊「Ver Avaluos」時打開窗口的左下角,則會看到可以將此數據導出爲ex​​cel。最簡單的方法是將數據以最少的操作(與網頁抓取相比)獲取到數據框中,將數據保存到Excel工作表中,然後使用gdata包中的read.xls命令讀入數據。這會自動將其保存在數據框中。