要去廣州工作了,所以抓取了廣州租房信息看一下,來源是某家廣州租房網。網上爬蟲代碼很多,對於簡單的網頁實現起來也很簡單,直接上核心代碼:
require(RCurl) ##載入包
require(XML)
rm(list = ls())
GZsource <- data.frame()
system.time(for (i in 1:100) {
if(i==1){webside ="http://xxxxxxxx.com/zufang/"}
else{webside = paste("http://xxxxxxxx/zufang/pg", i, "/", sep = "")}
url = getURL(webside, .encoding = "utf-8")
url_parse = htmlParse(url, encoding = "utf-8")
# 標題、鏈接
node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='info-panel']//a[@title]")
title_name = sapply(node, function(X) xmlGetAttr(X, "title"))
Encoding(title_name) = "UTF-8"
link = paste("http://sh.xxxxx.com", sapply(node, function(X) xmlGetAttr(X, "href")), sep = "")
# 小區、戶型、面積
node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='where']//span")
inf1 = sapply(node, xmlValue)
xiaoqu = inf1[(1:length(title_name))*5 - 4]
house_type = gsub("\\s+", "", inf1[(1:length(title_name))*5 - 2])
size = as.numeric(gsub("[^0-9]*$", "", inf1[(1:length(title_name))*5 - 1]))
# 區域、地段
node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='other']//a")
inf2 = sapply(node, xmlValue)
area = inf2[(1:length(title_name))]
#diduan = inf2[(1:length(title_name))*2 - 0]
# 價格
node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='price']/span[@class='num']")
price = as.numeric(gsub("[^0-9]", "", sapply(node, xmlValue)))
GZsource = rbind(GZsource, data.frame(title_name, link, xiaoqu, house_type, size, area, price, stringsAsFactors = FALSE))
Sys.sleep(1)
})
爲了分地區分析,我直接按照行政區分別進行的抓取,比如天河區如下:
Tianhe <- data.frame()
system.time(for (i in 1:100) {
if(i==1){
webside = "http://xxxxxxxx/zufang/tianhe/"}
else{webside = paste("http://xxxxxxxx/zufang/tianhe/pg", i, "/", sep = "")}
url = getURL(webside, .encoding = "utf-8")
url_parse = htmlParse(url, encoding = "utf-8")
# 標題、鏈接
node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='info-panel']//a[@title]")
title_name = sapply(node, function(X) xmlGetAttr(X, "title"))
Encoding(title_name) = "UTF-8"
link = paste("http://sh.xxxxx.com", sapply(node, function(X) xmlGetAttr(X, "href")), sep = "")
# 小區、戶型、面積
node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='where']//span")
inf1 = sapply(node, xmlValue)
xiaoqu = inf1[(1:length(title_name))*5 - 4]
house_type = gsub("\\s+", "", inf1[(1:length(title_name))*5 - 2])
size = as.numeric(gsub("[^0-9]*$", "", inf1[(1:length(title_name))*5 - 1]))
# 區域、地段
node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='other']//a")
inf2 = sapply(node, xmlValue)
area = inf2[(1:length(title_name))]
#diduan = inf2[(1:length(title_name))*2 - 0]
# 總價
node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='price']/span[@class='num']")
price = as.numeric(gsub("[^0-9]", "", sapply(node, xmlValue)))
# 單價
# node = getNodeSet(url_parse, path = "//div[@class='list-wrap']//div[@class='price-pre']")
# danjia = as.numeric(gsub("[^0-9]", "", sapply(node, xmlValue)))
Tianhe = rbind(Tianhe, data.frame(title_name, link, xiaoqu, house_type, size, area, price, stringsAsFactors = FALSE))
Sys.sleep(1)
})
Tianhe$District <- "天河區"
最後把每個區的合併起來就行:
GZhouse <- rbind(Tianhe,panyu,liwan,Yuexiu,haizhu,baiyun,luogang,huangpu,huadou,zengcheng)
最後結果如下
下一步就是在預處理後對房價進行可視化分析,看看現在不同區域的價格差異,等有時間再多找些數據以後多方位進行房價影響因素分析。
未完待續…