获得房地产的协调

时间:2012-04-25 19:41:11

标签: xml r web-scraping rcurl

我的任务是下载尽可能多的平板销售优惠。我有下载链接和其他东西的脚本,但是我无法获得单位的坐标(这对我来说至关重要,我的分析的主要内容)。

坐标在网站上可见(通过检查谷歌地图元素),但不在网站的来源中。

当我使用下面的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')

1 个答案:

答案 0 :(得分:2)

您的代码基本上是正确的,但它下载的页面中没有坐标。单击“地图”链接可激活Ajax请求以引入Google地图,您需要解析第二个迷你页面以查找坐标。我把它打包成一个函数。传入该网站页面的网址。它将下拉该页面,刮取数据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)
coords
相关问题