R语言抓取广州租房信息
来源:互联网 发布:微信朋友圈广告 知乎 编辑:程序博客网 时间:2024/04/20 14:03
要去广州工作了,所以抓取了广州租房信息看一下,来源是某家广州租房网。网上爬虫代码很多,对于简单的网页实现起来也很简单,直接上核心代码:
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)
最后结果如下
下一步就是在预处理后对房价进行可视化分析,看看现在不同区域的价格差异,等有时间再多找些数据以后多方位进行房价影响因素分析。
未完待续…
0 0
- R语言抓取广州租房信息
- 【R语言 爬虫】用R爬虫,爬取杭州安居客九堡租房信息
- python 爬虫抓取19楼租房信息
- 抓取赶集网租房信息 python3
- 大数据:广州租房信息统计,这些地方最便宜
- 爬取广州链家租房信息,并用tableau进行数据分析
- 广州租房提取住房公积金攻略
- R语言实现从豆瓣抓取数据
- R语言网页抓取入门-rvest包
- matlab 如何使用urlread函数抓取赶集网上的租房信息
- 利用R语言如何画出广州房价地图
- R语言 警告信息关闭
- Python爬链家网租房信息
- 租房直通车(租房信息搜索工具)
- R语言:网页抓取之get the data
- R语言:网页抓取之不同提取方法解析
- 如何使用R语言连接MySQL数据库,rjson,网页抓取
- R语言实现简单的网页数据抓取
- 十、SQLite数据库增删改查操作
- LeetCode - Unique Paths II
- nginx__安装过程记录(ubuntu14.04)
- jdk动态代理如此简单
- 微机原理 第1章 绪论
- R语言抓取广州租房信息
- else判断问题
- IOS-文件管理
- Shiro在web的授权检测(权限角色验证)
- mfc中web插件与c++之间相互调用方案
- Centos下JIRA破解安装
- android 之 linkToDeath和unlinkToDeath。(死亡代理)
- QCon上海2016 随想
- 十个利用矩阵乘法解决的经典题目