通过从多屏网页抓取信息获取信息

Getting information with web scraping from multiple screen web page

我想从网上获取一些企业的信息。大部分信息位于此页面中:http://appscvs.supercias.gob.ec/portalInformacion/sector_societario.zul, the page looks like this:

在此页面中,我必须单击选项卡 Busqueda de Companias,然后有趣的一面开始。当我点击时,我会看到下一个屏幕: 在此页面中,我必须设置选项 Nombre,然后我必须插入一个带有名称的字符串。例如,我将添加字符串 PROAÑO & ASOCIADOS CIA. LTDA.,我将看到下一个屏幕:

然后,我必须单击 Buscar,我将进入下一个屏幕:

在这个屏幕上我有这家企业的信息。然后,我必须单击 Informacion Estados Financieros 选项卡,我将看到下一个屏幕:

在这个最终屏幕中,我必须单击选项卡 Estado Situacion,然后我将在 Codigo de la cuenta contableNombre de la cuenta contableValor 列中获取来自企业的信息.我想将该信息保存在数据框中。当我必须设置元素 Nombre、插入字符串,然后 Buscar 并单击直到找到选项卡 Informacion Estados Financieros 时,我发现的大部分复杂的方面开始了。我尝试使用 rvest 包中的 html_sessionhtml_form,但元素是空的。

你能帮我解决这个问题吗?

查看 RSelenium

RSelenium 编码示例

这是一个独立的代码示例,使用问题中引用的网站。

观察:请不要运行此代码。

Why? Having 1k Stack users hit the web-site is a DDOS attack.


##介绍先决条件

下面的代码将安装 RSelenium,在 运行 之前,您需要:

  1. 安装 Firefox
  2. 添加 Selenium IDE 插件
  1. 安装RStudio[推荐]
  2. 创建项目并打开下面的代码文件

下面的代码会将您从第二页 [http://appscvs.supercias.gob.ec/portaldeinformacion/consulta_cia_param.zul] 带到您感兴趣的信息的最后一页...

有用的参考资料:

如果您对使用 RSelenium 感兴趣,我强烈建议您阅读以下参考资料,感谢 John Harrison 开发 RSelenium 包。

  • RSelenium Basics

http://rpubs.com/johndharrison/12843

  • RSelenium 无头浏览

http://rpubs.com/johndharrison/RSelenium-headless

  • RSelenium 插图

https://cran.r-project.org/web/packages/RSelenium/vignettes/basics.html

代码示例


# We want to make this as easy as possible to use
# So we need to install required packages for the user...
#
if (!require(RSelenium)) install.packages("RSelenium")
if (!require(XML)) install.packages("XML")
if (!require(RJSONIO)) install.packages("RSJONIO")
if (!require(stringr)) install.packages("stringr")
# Data
#
mainPage <- "http://appscvs.supercias.gob.ec/portalInformacion/sector_societario.zul"
businessPage <- "http://appscvs.supercias.gob.ec/portaldeinformacion/consulta_cia_param.zul"

# StartServer

# We assume RSelenium is not setup, so we check if the RSelenium
# server is available, if not we install RSelenium server.
checkForServer()

# OK. now we start the server
RSelenium::startServer()
remDr <- RSelenium::remoteDriver$new()

# We assume the user has installed Firefox and the Selenium IDE
# https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
#

# Ok we open firefix
remDr$open(silent = T) # Open up a firefox window...

# Now we open the browser and required URL...
# This is the page that matters...
remDr$navigate(businessPage)

# First things first on the first page, lets get the id's for the radio_button,
# name Element, and button. We need all three.
#
radioButton <- remDr$findElements(using = 'css selector', ".z-radio-cnt")
nameElement <- remDr$findElements(using = 'css selector', ".z-combobox-inp")
searchButton <- remDr$findElements(using = 'css selector', ".z-button-cm")

# Optional: we can highlight the radio elements returned
# lapply(radioButton, function(x){x$highlightElement()})
# Optional: we can highlight the nameElement returned
# lapply(nameElement, function(x){x$highlightElement()})
# Optional: we can highlight the searchButton returned
# lapply(searchButton, function(x){x$highlightElement()})

# Now we can select and press the third radio button
radioButton[[3]]$clickElement()
# We fill in the required name...
nameElement[[1]]$sendKeysToElement(list("PROAÑO & ASOCIADOS CIA. LTDA."))
# This is subtle but required the page triggers a drop down list, so rather than
# hitting the searchButton, we first select, and hit enter in the drop down menu...
selectElement <- remDr$findElements(using = 'css selector', ".z-comboitem-text")
selectElement[[1]]$clickElement()
# OK, now we can click the search button, which will cause the next page to open
searchButton[[1]]$clickElement()

# New Page opens...
#
# Ok, so now we first pull the list of buttons...
finPageButton <- remDr$findElements(using = 'class name', "m_iconos")
# Now we can press the required button to open the page we want to get too...
finPageButton[[9]]$clickElement()

# We are now on the required page.

我们现在位于目标页面[查看图片]

正在提取 table 值...

下一步是提取 table 值。为此,我们提取 .z-listitem css-selector 数据。现在我们可以检查以确认我们是否看到了数据行。我们这样做了,所以我们现在可以提取值 returned 并填充列表或数据框。

# Ok, now we need to extract the table, we identify and pull out the 
# '.z-listitem' and assign to modalWindow
modalWindow <- remDr$findElements(using = 'css selector', ".z-listitem")

# Now we can extract the lines from modalWindow... Now that each line is
# returned as a single line of text, so we split into three based on the
# line marker "/n'

lineText <- str_split(modalWindow[[1]]$getElementText()[1], '\n')
lineText

这里是结果:

> lineText <- stringr::str_split(modalWindow[[1]]$getElementText()[1], '\n')
> lineText
[[1]]
[1] "10"                                                                                                                                      
[2] "OPERACIONES DE INGRESO CON PARTES RELACIONADAS EN PARAÍSOS FISCALES, JURISDICCIONES DE MENOR IMPOSICIÓN Y REGÍMENES FISCALES PREFERENTES"
[3] "0.00"     

处理隐藏数据。

Selenium WebDriver 和 RSelenium 仅与网页的可见元素交互。如果我们尝试读取整个 table,我们只会 return table 可见(未隐藏)的项目。

我们可以通过滚动到 table 的底部来解决这个问题。由于滚动操作,我们强制填充 table。然后我们可以提取完整的 table.

# Select the .z-listbox-body

modalWindow <- remDr$findElements(using = 'css selector', ".z-listbox-body")

# Now we tell the window we want to scroll to the bottom of the table
# This triggers the table to populate all the rows

modalWindow[[1]]$executeScript("window.scrollTo(0, document.body.scrollHeight)")

# Now we can extract the complete table
modalWindow <- remDr$findElements(using = 'css selector', ".z-listitem")

lineText <- stringr::str_split(modalWindow[[9]]$getElementText(), '\n')
lineText

###代码的作用。

上面的代码示例是独立的。我的意思是它应该安装您需要的一切,包括所需的软件包。安装依赖的 R 包后,R 代码将调用 checkForServer(),如果未安装 Selenium,则调用将安装它。 这可能需要一些时间

我的建议是你逐步完成代码,因为我没有加入任何延迟(在生产中你会想要),还要注意我没有优化速度而是为了一点点清晰度 [从我的角度来看]。 ..

显示代码可用于:

  • Mac OS X 10.11.5
  • RStudio 0.99.893
  • R 版本 3.2.4 (2016-03-10) -- “非常安全的菜肴”