当我使用Rvest
工具从新闻网站抓取链接时,我经常偶然发现重定向到另一个链接的链接。在这些情况下,我只能抓取第一个链接,而第二个链接是实际包含数据的链接。例如:
library(dplyr)
library(rvest)
scraped.link <- "http://www1.folha.uol.com.br/folha/dinheiro/ult91u301428.shtml"
article.title <- read_html(scraped.link) %>%
html_nodes('body') %>%
html_nodes('.span12.page-content') %>%
html_nodes('article') %>%
html_nodes('header') %>%
html_nodes('h1') %>%
html_text()
article.title
#> character(0)
redirected.link <- "https://www1.folha.uol.com.br/mercado/2007/06/301428-banco-central-volta-a-intervir-no-mercado-para-deter-queda-do-cambio.shtml"
article.title <- read_html(redirected.link) %>%
html_nodes('body') %>%
html_nodes('.span12.page-content') %>%
html_nodes('article') %>%
html_nodes('header') %>%
html_nodes('h1') %>%
html_text()
article.title
#> "Banco Central volta a intervir no mercado para deter queda do câmbio"
有没有办法使用第一个链接获取第二个链接?该网站仅保留第一个。
是的,页面通过javascript'location.replace'重定向,所以只需使用正则表达式在脚本标签的html文本中"location.replace"的第一个实例之后提取第一个引用的项目:
library(dplyr)
library(rvest)
scraped.link <- "http://www1.folha.uol.com.br/folha/dinheiro/ult91u301428.shtml"
link.regex <- "(.*?location[.]replace.*?")(.*?)(".*)"
read_html(scraped.link) %>%
html_nodes('script') %>%
html_text() %>%
gsub(link.regex, "\2", .)
#> [1] "http://www1.folha.uol.com.br/mercado/2007/06/301428-banco-central-volta-a-intervir-
#> no-mercado-para-deter-queda-do-cambio.shtml"