javascript - 使用 JavaScript 链接抓取网页

标签 javascript r web web-scraping

我正在使用 R 进行网页抓取。我需要的信息在this webpage.的链接中 但是当我点击时,链接会转到我所在的同一页面。如何通过这些其他链接抓取信息,直到获得包含我需要的信息的表格?我几个月前开始使用 R,我知道 httr、Curl 和其他包,但我无法抓取这个网页。我需要这样的输出(通过单击“Todo el territorio”和Tipo de estudios:“Bachillerato”):

Provincia|Localidad|Denominacion Generica|Denominacion Especifica|Codigo|Naturaleza
Almería|Adra|Instituto de Educación Secundaria|Abdera|04000110|Centro público
Almería|Adra|Instituto de Educación Secundaria|Gaviota|04000134|Centro público

...

这将是我使用 Selenium 包的通用脚本,但它不起作用,我接受任何选项:

library(RSelenium)
library(XML)
library(magrittr)

RSelenium::checkForServer()
RSelenium::startServer()
remDrv <- RSelenium::remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "chrome")
remDrv$open()

remDrv$navigate('https://www.educacion.gob.es/centros/selectaut.do')
remDrv$findElement(using = "xpath", "//select[@name = '.listado-inicio']/option[@value = ('02','00')]")$clickElement()

...

或者类似的东西。我在 stackoverflow 中寻找其他主题时发现了与此脚本类似的内容,但我没有得到任何内容。我接受其他脚本的其他解决方案。 非常感谢。

最佳答案

使用“RSelenium”导航您可以执行的操作:

library(RSelenium)
library(rvest)
#start RSelenium
checkForServer()
startServer()
remDr <- remoteDriver()
remDr$open()

remDr$navigate('https://www.educacion.gob.es/centros/selectaut.do')

#Click on the todo el territorio link
remDr$findElement(using = "xpath", "//a[text()='Todo el territorio']")$clickElement()

#select the Bachillerato option (has a value of 133) and click on the search button
remDr$findElement(using = "xpath", "//select[@id='comboniv']/option[@value='133']")$clickElement()
remDr$findElement(using = "xpath", "//input[@id='idGhost']")$clickElement()

#Click on the show results button
remDr$findElement(using = "xpath", "//input[@title='Buscar']")$clickElement()

#parse the html and get the table
doc <- htmlParse(remDr$getPageSource()[[1]],encoding="UTF-8")
data <- readHTMLTable(doc)$matcentro

关于javascript - 使用 JavaScript 链接抓取网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30195092/

相关文章:

arrays - R 中球体的网格或数组表示

web - 我可以在中国测试我的 web 应用程序以查看被阻止的内容吗?

JavaScript 自动更改 src 和 []

javascript - 让 JS 自动完成计算器应用程序的数学方程

javascript - foreach 元素中的 setTimeout 不起作用

javascript - API 是否永久存储从网站提取的数据?

javascript - 预加载网站

javascript - 在数组映射的最后一个元素上自动触发单击事件处理程序。 Reactjs

R:将字符变量编码为数字

r - 填补 R 中时间序列数据的空白