2016-03-28 78 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命令读入数据。这会自动将其保存在数据框中。