【问题标题】:Getting information with web scraping from multiple screen web page从多屏网页抓取网页信息
【发布时间】:2016-08-28 10:33:16
【问题描述】:

我正在尝试从 Internet 上获取有关企业的一些信息。大部分信息位于这个页面:http://appscvs.supercias.gob.ec/portalInformacion/sector_societario.zul,页面长这样:

在这个页面中,我必须点击标签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,但元素为空。

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

【问题讨论】:

  • 检查硒。它是为这类事情而设计的。
  • 你应该把这个问题缩小到一两个步骤。仅身份验证步骤就是一个单一大小的问题。
  • 可能不是一个非常复杂的方法并且仅适用于这种情况,但您可以尝试使用所有可用的字符串并使用&,; 将它们连接起来。如果这样可行,您可以同时拥有整个表格(由于某种原因,网站无法加载,所以我无法尝试)。

标签: r rvest


【解决方案1】:

RSelenium 编码示例

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

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

为什么? 有 1k Stack 用户访问网站是 DDOS 攻击。


##介绍先决条件

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

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

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

有用的参考资料:

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

  • RSelenium 基础知识

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.

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

正在提取表值...

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

# 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 只与网页的可见元素交互。如果我们尝试读取整个表格,我们将只返回可见(未隐藏)的表格项。

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

# 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) -- “非常安全的菜肴”

【讨论】:

    【解决方案2】:

    查看RSelenium

    【讨论】:

    • 感谢@vijucat 我已经寻找并检查了RSelenium,但我对它以及它与我要解决的问题的关系没有清楚的了解。
    • @Duck - 请参阅提供的示例。我希望它能让您了解如何使用 RSelenium 做您需要的事情。
    • @vijucat - 谢谢,我希望答案能帮助 Duck 指明正确的方向。一旦我们收到回复,我很乐意扩展答案。
    • @Technophobe01谢谢楼主,你的帮助很有帮助。我会给你分数,但我可能需要一些额外的帮助。我们如何沟通?
    • @Technophobe01我想开始解决问题或隐藏数据,您的贡献将非常有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    相关资源
    最近更新 更多