gpt4 book ai didi

javascript - 使用 JavaScript 链接抓取网页

转载 作者:行者123 更新时间:2023-11-28 00:19:01 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com