我的任务是下载尽可能多的平面销售报价。我有下载链接和其他东西的脚本,但我无法获得平坦的坐标(这对我至关重要,我的分析要点)。
坐标在网站上可见(通过检查谷歌地图元素),但在网站的源代码中不可见。
当我使用下面的R代码时,我得到一个空的列表,无论我是使用XML还是RCurl包,都没有区别。
你认为可以使用R来实现这一点吗?或者我应该考虑其他编程语言(例如,Python?)
library(XML)
library(RCurl)
url<-'http://en.otodom.pl/flat-poznan-jezyce-2720m2-2-rooms-125000-pln-id13250586.html'
doc<-htmlParse(url,encoding='utf-8')
xpathApply(doc,'//div[@id="googleMap"]',xmlGetAttr,'data-map')
#the same result
doc<-getURL(url, httpheader = c('User-Agent' = "Informative string with your contact info"),.encoding='utf-8')
doc<-htmlParse(doc,encoding='utf-8')
xpathApply(doc,'//div[@id="googleMap"]',xmlGetAttr,'data-map')
# the same here
doc<-getURL(url, followlocation=T)
doc<-htmlParse(doc,encoding='utf-8')
xpathApply(doc,'//div[@id="googleMap"]',xmlGetAttr,'data-map')发布于 2012-04-26 07:43:56
您的代码基本上是正确的,但它下载的页面中没有坐标。单击Maps链接将激活一个Ajax请求,以拉入Google Map,并且您需要解析第二个迷你页面来查找坐标。我已经将其打包到一个函数中。传入该站点上页面的url。它将下拉页面,抓取数据id,调用Google Map并从中抓取坐标。
library(XML)
library(RCurl)
get.coords <- function(url) {
doc<-htmlParse(url,encoding='utf-8')
data.ins.id <- getNodeSet(doc,'//@data-ins-id')
data.ins.id <- as.character(data.ins.id[[1]]["data-ins-id"])
mapurl <- paste('http://en.otodom.pl/?mod=show&act=showMap&insId=',
data.ins.id,
sep='')
doc <- htmlParse(mapurl,encoding='utf-8')
result <- getNodeSet(doc,'//@data-map')
result <- as.character(result[[1]]["data-map"])
return(strsplit(result, "|", fixed=TRUE)[[1]][c(1,2)])
}
url <- 'http://en.otodom.pl/flat-poznan-jezyce-2720m2-2-rooms-125000-pln-id13250586.html'
coords <- get.coords(url)
coordshttps://stackoverflow.com/questions/10322608
复制相似问题