【问题标题】:Scraping a web with javascript links使用 javascript 链接抓取网页
【发布时间】:2015-05-12 15:18:20
【问题描述】:

我正在使用 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 中找到了与此脚本类似的东西来寻找其他主题,但我什么也没得到。我接受其他脚本的其他解决方案。 非常感谢。

【问题讨论】:

  • 您能展示一下您目前使用的代码吗?

标签: javascript r web web-scraping


【解决方案1】:

使用“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

【讨论】:

  • 现在我正在尝试点击链接以获取数据data.frame的每所学校(Abdera,Gaviota ...)的以下页面中的信息......(Identificación,ubicación,Tipificación )。我不能再这样做了。你知道一个简单的方法来获得这个吗?其他问题。你怎么知道 133 是“Bachillerato”选项的号码?你知道一些使用“xpath”等命令的手册吗?再次非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 2019-02-16
  • 1970-01-01
  • 2020-11-17
  • 2023-03-26
  • 1970-01-01
  • 2018-08-01
相关资源
最近更新 更多