我正试图从这个网站上搜集材料:http://www.appliedsolutions.org/site/308/Local-Government/Local-Government-Affiliates
具体来说,我有兴趣从页面源代码第598行附近的javascript中提取值:
{
"title": 'Coconino County',
"lat": '35.7714',
"lng": '-111.5111',
"description": 'Coconino County, AZ <br/> <a href="http://www.coconino.az.gov/" target="_blank"> http://www.coconino.az.gov/</a> <br/> '
}
,
{
"title": 'City of Flagstaff',
"lat": '35.1981',
"lng": '-111.6506',
"description": 'City of Flagstaff, AZ <br/> <a href="http://www.flagstaff.az.gov/ " target="_blank"> http://www.flagstaff.az.gov/ </a> <br/> '
}
理想情况下,我想将“title”,“lat”和“lng”值带入R data.frame
。
我使用R中的readLines
函数来读取页面,但是在减少html以隔离我需要的数据方面遇到了麻烦。
答案 0 :(得分:5)
这是使用RSelenium
包的一种方式。
## Get RSelenium going and navigate to page, retrieve source
require(RSelenium)
RSelenium::checkForServer()
RSelenium::startServer()
remDr <- remoteDriver()
remDr$open()
remDr$setImplicitWaitTimeout(3000)
remDr$navigate("http://www.appliedsolutions.org/site/308/Local-Government/Local-Government-Affiliates")
编辑:每个@jdharrison建议更简单
appData <- remDr$executeScript("return markers;")
dat <- do.call(rbind.data.frame, appData)
dat <- dat[,c("title","lat","lng")]
> head(dat)
lat lng title
35.7714 -111.5111 Coconino County
35.1981 -111.6506 City of Flagstaff
34.8697 -111.7603 City of Sedona
34.6503 -112.4147 Yavapai County
32.64 -117.0833 City of Chula Vista
38.8056 -123.0161 City of Cloverdale