我是怎么找房子的

找房子、搬家一直是很麻烦的一件事,如果使用爬虫会不会好一些呢?

01、网站选取

明确不想找中介了,所以选择赶集网西湖区个人房源作为主要信息搜集网站

我是怎么找房子的_第1张图片
赶集网

02、爬取内容

先明确一下要爬取的内容:时间、价格、地址、格局都是很重要的信息

我是怎么找房子的_第2张图片
爬取内容

03、爬第一页试验一下

步骤一、基础信息

先把标题、信息、地址都爬下来


我是怎么找房子的_第3张图片
基础信息
library(rvest)
library(stringr)
url<-"http://hz.ganji.com/fang1/xihu/a1/"
web <- read_html(url)
xinxi<-html_nodes(web,'.address , .size , .title-font')%>%html_text()
xinxi<-gsub("\n","",xinxi)
xinxi<-gsub(" ","",xinxi)
length(xinxi)
#--------------------------------------------------------->title
title<- xinxi %>% .[seq(1,length(xinxi),3)]
title
house<-as.data.frame(title)
message<- xinxi %>% .[seq(2,length(xinxi),3)]
message
area<- xinxi %>% .[seq(3,length(xinxi),3)]
area
house<-cbind(house,message,area)
house<-house[1:34,]

步骤二、价格、时间

然后把价格和时间也爬下来


我是怎么找房子的_第4张图片
价格、时间
#--------------------------------------------------------->price
price<- html_nodes(web,'.num')%>%html_text()
price<-price%>% .[c(2:35)]
house<-cbind(house,price)
#--------------------------------------------------------->time
time<- html_nodes(web,'.time')%>%html_text()
house<-cbind(house,time)

04、建立循环

租房是有时间要求的,帖子的时间太久了就不合适了,所以打算爬取前30页的租房信息。

我是怎么找房子的_第5张图片
1368条租房信息
site1 <- "http://hz.ganji.com/fang1/xihu/a1o"
site2 <- "/"
page <- 2
ganji<-house
for(page in 2:30){
  site <- paste(site1,page,site2,sep="")
  web <- read_html(site)
  xinxi<-html_nodes(web,'.address , .size , .title-font')%>%html_text()
  xinxi<-gsub("\n","",xinxi)
  xinxi<-gsub(" ","",xinxi)
  #--------------------------------------------------------->title
  title<- xinxi %>% .[seq(1,length(xinxi),3)]
  house1<-as.data.frame(title)
  message<- xinxi %>% .[seq(2,length(xinxi),3)]
  area<- xinxi %>% .[seq(3,length(xinxi),3)]
  house1<-cbind(house1,message,area)
  house1<-house1[1:46,]
  #--------------------------------------------------------->price
  price<- html_nodes(web,'.num')%>%html_text()
  price<-price%>% .[c(2:47)]
  house1<-cbind(house1,price)
  #--------------------------------------------------------->time
  time<- html_nodes(web,'.time')%>%html_text()
  time<-time%>% .[c(1:46)]
  house1<-cbind(house1,time)
  ganji<-rbind(ganji,house1)
}

05、开始找房子了

直接在excel里操作很方便:

(1)先按时间降序,6月15以前的招租信息就不要了
(2)考虑现在的房价,价格低于1000的字段也不要
(3)·······根据跟人条件看吧

所有赶集网的租房信息就看完了

06、隔三差五要运行一遍程序

因为网站信息不停在变化,所以隔三差五运行一遍,会有新的租房信息的。

你可能感兴趣的:(我是怎么找房子的)