如何在阅读R中的行之前等待网页加载?
我用R来刮一些网页。其中一个页面重定向到新页面。当我像这样在这个页面上使用如何在阅读R中的行之前等待网页加载?,r,redirect,web-scraping,R,Redirect,Web Scraping,我用R来刮一些网页。其中一个页面重定向到新页面。当我像这样在这个页面上使用readLines时 test <- readLines('http://zfin.org/cgi-bin/webdriver?MIval=aa-markerselect.apg&marker_type=GENE&query_results=t&input_name=anxa5b&compare=contains&WINSIZE=25') test我不知道如何等待重定向,但在
readLines
时
test <- readLines('http://zfin.org/cgi-bin/webdriver?MIval=aa-markerselect.apg&marker_type=GENE&query_results=t&input_name=anxa5b&compare=contains&WINSIZE=25')
test我不知道如何等待重定向,但在重定向之前的网页源代码中,您可以看到(在脚本标记中)一个javascript函数replaceLocation
,其中包含重定向路径:replaceLocation(\“/ZDB-GENE-030131-9076\”
然后我建议您解析代码并获得此路径。
以下是我的解决方案:
library(RCurl)
library(XML)
url <- "http://zfin.org/cgi-bin/webdriver?MIval=aa-markerselect.apg&marker_type=GENE&query_results=t&input_name=anxa5b&compare=contains&WINSIZE=25"
domain <- "http://zfin.org"
doc <- htmlParse(getURL(url, useragent='R'))
scripts <- xpathSApply(doc, "//script", xmlValue)
script <- scripts[which(lapply(lapply(scripts, grep, pattern = "replaceLocation\\([^url]"), length) > 0)]
# > script
# [1] "\n \n\t \n\t replaceLocation(\"/ZDB-GENE-030131-9076\")\n \n \n\t"
new.url <- paste0(domain, gsub('.*\\"(.*)\\".*', '\\1', script))
readLines(new.url)
库(RCurl)
库(XML)
网址