爬去链家房价数据

主要是用了rvest进行爬虫。使用这个爬去数据比较方便快捷。

library("xml2")

library("rvest")

library("dplyr")

library("stringr")

#对爬取页数进行设定并创建数据框

i<-1:100

house_inf<-data.frame()

#使用for循环进行批量数据爬取(发现url的规律,写for循环语句)

for (i in 26:64){

  web<- read_html(str_c("http://gz.fang.lianjia.com/loupan/pg",i),encoding="UTF-8")

  #用SelectorGadget定位节点信息并爬取房名

  house_name<-web%>%html_nodes("div.info-panel div.col-1 a ")%>%html_text()

  #爬取二手房基本信息并消除空格

  #house_basic_inf<-web%>%html_nodes(".other ")%>%html_text()

  #house_basic_inf<-str_replace_all(string = house_basic_inf,pattern = "\t",replacement = " ")

  #house_basic_inf= str_replace_all(string = house_basic_inf,pattern = "\n",replacement = " ")

  #house_basic_inf= str_replace_all(string = house_basic_inf,pattern = "            ",replacement = ",")
   #SelectorGadget定位节点信息爬取地址

  house_address<-web%>%html_nodes(".region")%>%html_text()

  #SelectorGadget定位节点信息爬取户型

  house_totalprice<-web%>%html_nodes(".area")%>%html_text()

  house_totalprice=str_replace_all(string = house_totalprice,pattern = "\n",replacement = " ")

  house_totalprice=str_replace_all(string = house_totalprice,pattern = "\t",replacement = " ")

  house_totalprice=str_replace_all(string = house_totalprice,pattern = " ",replacement = "")
   #SelectorGadget定位节点信息爬取单价

  house_unitprice<-web%>%html_nodes(".average")%>%html_text()

  house_unitprice=str_replace_all(string = house_unitprice,pattern = "\n",replacement = "")

  house_unitprice=str_replace_all(string = house_unitprice,pattern = "\t",replacement = " ")

  house_unitprice=str_replace_all(string = house_unitprice,pattern = " ",replacement = "")
  #创建数据框存储以上信息

  house<-data_frame(house_name,house_address,house_totalprice,house_unitprice)
  colnames(house)=c("楼盘名","地址","户型","均价")
  house_inf<-rbind(house_inf,house)
  print(i)
}

#将数据写入csv文档

write.csv(house_inf,file="D:/Rdata/datasets/house_inf.csv")



# 二手房https://gz.lianjia.com/ershoufang/


#加载所需的包

library("xml2")

library("rvest")

library("dplyr")

library("stringr")

#对爬取页数进行设定并创建数据框

i<-1:100

house_inf_ershou<-data.frame()

#使用for循环进行批量数据爬取(发现url的规律,写for循环语句)

for (i in 17:100){
  house=reptile(i)
  house_inf_ershou=rbind(house_inf_ershou,house)
  print(i)

}

#将数据写入csv文档

write.csv(house_inf,file="D:/Rdata/datasets/house_inf.csv")


# 写一个爬取函数
reptile=function(i){
  web<- read_html(str_c("https://gz.lianjia.com/ershoufang/",i),encoding="UTF-8")

  #用SelectorGadget定位节点信息并爬取房名

  house_name<-web%>%html_nodes(".houseInfo a")%>%html_text()

  #爬取二手房基本信息并消除空格

  house_basic_inf<-web%>%html_nodes(".houseInfo")%>%html_text()

  house_basic_inf<-str_replace_all(house_basic_inf," ","")

  #SelectorGadget定位节点信息爬取地址

  house_address<-web%>%html_nodes(".positionInfo a")%>%html_text()

  #SelectorGadget定位节点信息爬取总价

  house_totalprice<-web%>%html_nodes(".totalPrice")%>%html_text()

  #SelectorGadget定位节点信息爬取单价

  house_unitprice<-web%>%html_nodes(".unitPrice span")%>%html_text()

  #创建数据框存储以上信息

  house<-data_frame(house_name,house_basic_inf,house_address,house_totalprice,house_unitprice)

  return(house)
}

深圳楼盘价格

爬去链家房价数据_第1张图片
image

深圳二手房价格

爬去链家房价数据_第2张图片
image

对楼盘简单分析

爬去链家房价数据_第3张图片
image

二手房分布

价格情况

爬去链家房价数据_第4张图片
image

一些简单结论:

深圳二手房均价:494.23

深圳二手房中位数:385

小产权房均价:203.2 中位数:150

中产权房均价:391 中位数:375

大产权房均价 1063 中位数 1168

你可能感兴趣的:(爬去链家房价数据)