在R中通过先浏览JavaScript模块进行网络搜刮。

问题描述 投票:0回答:1

我查找了各种问题和答案,但遗憾的是,我找到的问题都没有处理与我类似的情况。在一个典型的问题中,当网站加载时,JavaScript表直接建立起来。然而,在我的情况下,我首先要通过JavaScript模块进行导航,并选择几个标准,然后才能得到想要的结果。

这是我的案例。我必须从这个网站上获取各种货币的汇率。www.globocambio.co. 要做到这一点,我有(1)导航到 "我要哥伦比亚比索"(2)选择货币(例如, "智利比索"),(3)和收集目的地(如:"El Dorado国际机场")。"El Dorado国际机场"). 只有这样才会加载相应的汇率。请看这个 截图 以作说明。我把三个选择步骤标记为红色。绿色是我想搜刮不同货币的数据点。

我对JavaScript不是很熟悉,但我试着去了解是怎么回事。这是我发现的。

  1. 使用Chrome DevTools,我调查了加载汇率时的网络活动。有一个名为 "GetPrice "的XHR,使用这个URL请求价格。https://reservations.globocambio.co/DesktopModules/GlobalExchange/API/Widget/GetPrice 并使用以下表格数据 ISOAOrigen=CLP&cantidadOrigen=9000&ISOADestino=COP&cantidadDestino=0&centerId=27&operationType=OperationTypesBuying
  2. 我明白,表格数据包含我最初手动选择的信息。
    1. operationType=OperationTypesBuying这就是 "我要哥伦比亚比索" 选择权
    2. ISOAOrigen=CLP这就是 "智利比索"
    3. centerId=27这就是 "埃尔多拉多国际机场"
  3. 服务器对我的请求做出了以下信息的响应。

    {“MonedaOrigen":{"ISOA":"CLP","Nombre":null,"Margen":0.1630000000,"Tramo":0.0,"Fixing":2.9000000000},"CantidadOrigen":9000.00,"MonedaDestino":{"ISOA":"COP","Nombre":null,"Margen":0.0,"Tramo":0.0,"Fixing":0.0},"CantidadDestino":21845.70,"TipoCambio":2.42730000000000000000,"MargenOrigen":0.0,"TramoOrigen":0.0,"FixingOrigen":0.0,"MargenDestino":0.0,"TramoDestino":0.0,"FixingDestino":0.0,"IdCentro":"27","Comision":null,"ComisionTramoSuperior":null,"ComisionAplicada":{"CodigoMoneda":null,"CodigoTipoMoneda":0,"ComisionFija":0.0,"ComisionVariable":0.0,"TramoInicio":0.0,"TramoFin":null,"Orden”:0}}

  4. 从这个响应来看: "TipoCambio":2.42730000000000000000 然后正在网站上用这行HTML代码写。<span id="spTipoCambioCompra">2.427300</span>

  5. 这意味着 "TipoCambio" 是我要找的值。

所以,我必须通过R与服务器进行某种方式的通信,使用表格数据作为输入变量。谁能告诉我如何做到这一点?我的意思是,我必须将URL的 https://reservations.globocambio.co/DesktopModules/GlobalExchange/API/Widget/GetPrice 与表格数据"ISOAOrigen=CLP&cantidadOrigen=9000&ISOADestino=COP&cantidadDestino=0&centerId=27&operationType=OperationTypesBuying"不知何故,但我不知道它是如何工作的。

任何帮助将被感激!

更新一下。

我仍然不知道如何解决上述问题,但。然而,我尝试用小步骤来解决它.使用RSelenium,我目前正试图找出如何点击选项 "我要哥伦比亚比索". 我的想法是使用下面的代码。

library(RSelenium)
remDr <- RSelenium::remoteDriver(remoteServerAddr = "localhost",
                                 port = 4445L,
                                 browserName = "chrome")
remDr$open()
remDr$navigate("https://www.globocambio.co/en/home")
webElem <- remDr$findElement("id", "tabCompra") #What is wrong here?
webElem$clickElement() # Click on "I WANT COLOMBIAN PESO"

但我在执行后得到一个错误信息 webElem <- remDr$findElement("id", "tabCompra"):

Selenium message:no such element: 无法定位元素。{"method": "css selector", "selector": "#tabCompra"}。 (会话信息:chrome=81.0.4044.113) 关于此错误的文档,请访问: https:/www.seleniumhq.orgexceptionsno_such_element.html ... 错误。 摘要:NoSuchElement Detail。类:org.openqa.selenium.NoSuchElementException 更多细节:运行errorDetails方法。

我在这里做错了什么?

javascript r web-scraping rselenium
1个回答
0
投票

我用Python中的selenium解决了我的问题。

from selenium import webdriver
driver = webdriver.Firefox(executable_path = '/your_path/geckodriver')

driver.get("https://www.globocambio.co/en/")
driver.switch_to.frame("iframeWidget");

elem = driver.find_element_by_id('tabCompra')
elem.click()

elem = driver.find_element_by_id('inputddlMonedaOrigenCompra')
elem.click()
elem.send_keys(Keys.CLEAR)
elem.send_keys("Chilean Peso")
elem.send_keys(Keys.ENTER)
elem.send_keys(Keys.ARROW_DOWN)
elem.send_keys(Keys.RETURN)

elem = driver.find_element_by_id('info-change-compra')
print(elem.text)
© www.soinside.com 2019 - 2024. All rights reserved.