繁体   English   中英

使用JavaScript链接抓取网页

[英]Scraping a web with javascript links

我正在使用R进行网页抓取。 我需要的信息在此网页的链接中 但是,当我单击时,链接将转到我所在的页面。 在获得包含所需信息的表格之前,如何在其他链接后面抓取信息? 几个月前,我开始使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM