【问题标题】:How to scrape this links with follow_link in R?如何使用 R 中的 follow_link 抓取此链接?
【发布时间】:2018-03-29 18:55:53
【问题描述】:

我正在学习如何使用 R 进行网络抓取。在这种情况下,我使用包“rvest”和一个名为 follow_link 的特定函数。

这个想法是获取具有多个链接的网页的信息。我希望我的代码输入这些链接并获取其中的表格。

这是代码:

library(rvest)
s <- html_session("http://fccee.uvigo.es/es/profesorado.html")
link <- c("Dereito Privado", "Economia Financieira e Contabilidade", "Matemáticas",
      "Estadística e Investigación Operativa", "Economía Aplicada", "Fundamentos da Análise Ec. e Hª e Institucións Económicas",
      "Informática", "Organización de Empresas e Marketing", "Socioloxía, Ciencia Política e da Administración e Filosofía")
n <- length(link) #number of pages
datos <- list()
for (i in 1:n){

    s <- s %>% follow_link(link[i])
    datos[[(i)]] <- s %>% html_nodes(".lista_fccee") %>% html_table()
    s <- s %>% back()}

问题是我得到了这个错误:没有链接有文字'Matemáticas'。 我认为问题与文本重音标记有关,因为前两个链接没有问题。

这可能是一个非常基本的问题,但我没有找到有关此特定错误的任何信息。

提前谢谢你!

【问题讨论】:

  • 请不要抓取这些页面来建立与该机构无关的电子邮件垃圾邮件列表或电子邮件目录。当我看到的所有重要信息都是姓名和电子邮件地址时,我会高度怀疑其意图。
  • 这不是我的本意。我在那所大学学习,这就是为什么我选择那个网页作为例子来抓取。

标签: r web-scraping rvest


【解决方案1】:

正如您所怀疑的,问题在于特殊字符(带重音的 a)。您可以使用以下代码查看 R 如何查看链接名称:

library(rvest)
top_url = "http://fccee.uvigo.es/es/profesorado.html"
page = read_html(top_url)
links = page %>% html_nodes("a") %>% html_text()
links
#> ...
#> [44] "Matemáticas"
#> ...

这最终成为一个复杂的编码问题,我不知道如何处理。因此,这里有另一种获取数据的方法。

library(rvest)
top_url = "http://fccee.uvigo.es/es/profesorado.html"
page = read_html(top_url)
links = page %>% 
  html_nodes(".listado_fccee li a") %>% 
  html_attr("href")
datos <- list()
for(i in links){
  datos[[length(datos)+1]] <- i %>% 
  paste0("http://fccee.uvigo.es",.) %>%
  read_html() %>%
  html_nodes(".lista_fccee") %>% 
  html_table()
}

您没有使用会话,而是在第一页中阅读,从具有部门链接的 div 类listado_fccee 中提取所有链接。然后,您可以像以前一样阅读每个链接并获取表格,并将它们添加到您的列表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2020-11-10
    • 2014-12-07
    • 1970-01-01
    相关资源
    最近更新 更多