美文网首页
使用爬虫获取南京地铁一号线周边的房源信息并作统计

使用爬虫获取南京地铁一号线周边的房源信息并作统计

作者: 一只烟酒僧 | 来源:发表于2024-07-29 22:33 被阅读0次
.libPaths("d://R_package/R.4.2.1/")
# 加载必要的库
library(httr)
library(rvest)
library(stringr)
library(dplyr)
library(ggplot2)
library(ggprism)
library(purrr)
library(tidyr)
library(forcats)
library(plyr)

# 函数来获取页面内容
get_page_content <- function(url) {
  Sys.sleep(5)  # 添加延迟以避免被封IP
  response <- GET(url, user_agent("Mozilla/5.0"))
  content(response, "text")
}

# 函数来解析页面内容
parse_page <- function(html_content) {
  page <- read_html(html_content)
  
  title <- page %>% html_nodes('li.house-cell.realverify div.des h2 a') %>% html_text(trim = TRUE)
  url <- page %>% html_nodes('li.house-cell.realverify div.des h2 a') %>% html_attr("href")
  size <- page %>% html_nodes('li.house-cell.realverify div.des p.room') %>% html_text(trim = TRUE)
  info <- page %>% html_nodes('li.house-cell.realverify div.des p.infor') %>% html_text(trim = TRUE)
  price <- page %>% html_nodes('li.house-cell.realverify div.money') %>% html_text(trim = TRUE)
  author <- page %>% html_nodes('li.house-cell.realverify div.jjr') %>% html_text(trim = TRUE)
  
  data.frame(title = title, url = url, size = size, info = info, price = price, author = author)
}

# 获取所有页面的数据
zhengzu_info <- map_df(1:70, function(page_num) {
  url <- paste0("https://nj.58.com/zufang/sub/l47/pn", page_num, "/")
  html_content <- get_page_content(url)
  df <- parse_page(html_content)
  print(paste("完成第", page_num, "页信息提取,共获得", nrow(df), "条信息", sep = ""))
  df
})

# 保存原始数据
saveRDS(zhengzu_info, "zhengzu.rds")

zhengzu_info_raw<-readRDS("zhengzu.rds")

# 数据清洗
yihaoxianzhan <- c("迈皋桥站", "红山动物园站", "南京站站", "新模范马路站", 
                   "玄武门站", "鼓楼站", "珠江路站", "新街口站", 
                   "张府园站", "三山街站", "中华门站", "安德门站", 
                   "天隆寺站", "软件大道站", "花神庙站", "南京南站站", 
                   "双龙大道站", "河定桥站", "胜太路站", "百家湖站", 
                   "小龙湾站", "竹山路站", "天印大道站", "龙眠大道站", 
                   "南医大站", "南京交院站", "中国药科大学站")

zhengzu_info <- zhengzu_info_raw %>%
  mutate(
    title = str_remove_all(title, " "),
    num =str_split(size, " {1,}", simplify = TRUE)%>%.[,1],
    area = str_split(size, " {1,}", simplify = TRUE)%>%.[,2],
    info = str_remove_all(info, " {1,}"),
    dir1 = str_split(info, "\\n{1,}", simplify = TRUE)[,1],
    dir2 = str_split(info, "\\n{1,}", simplify = TRUE)[,3],
    dir_tmp = str_split(info, "\\n{1,}", simplify = TRUE)[,4],
    dir3 = str_remove(str_extract(dir_tmp, "距1号线-[\\p{Han}]{1,}"), "距1号线-"),
    dir4 = as.numeric(str_remove(str_extract(dir_tmp, "[0-9]{1,}m"), "m")),
    author = str_remove_all(author, " {1,}"),
    author1 = str_split(author, "\\n{1,}", simplify = TRUE)[,1],
    author2 = str_split(author, "\\n{1,}", simplify = TRUE)[,2],
    author3 = str_split(author, "\\n{1,}", simplify = TRUE)[,3]
  ) %>%
  select(title, num, area, dir1, dir2, dir3, dir4, price, author1, author2, author3, url) %>%
  mutate(
    area = as.numeric(str_extract(area, "[0-9\\.]{1,}")),
    price = as.numeric(str_remove(price, "元/月"))
  )

yihaoxianzhan <- intersect(yihaoxianzhan, unique(paste0(zhengzu_info$dir3,"站")))
zhengzu_info$dir3 <- factor(paste0(zhengzu_info$dir3,"站"), levels = yihaoxianzhan)

# 保存清洗后的数据
saveRDS(zhengzu_info, "zhengzu_info_cleandata.rds")  

# 去除重复项
zhengzu_info <- zhengzu_info[!duplicated(zhengzu_info$title),]

#统计
#1、计算均房价
zhengzu_summary<-zhengzu_info%>%
  mutate(ave_price=price/area)%>%
  filter(!is.na(dir3))%>%
  mutate(dir3=factor(dir3,levels = yihaoxianzhan))
zhengzu_num<-ddply(zhengzu_summary,.(dir3),nrow)
ggplot(zhengzu_summary%>%filter(!is.na(dir3)),aes(x=dir3,y=ave_price))+
  geom_boxplot()+
  geom_text(data = zhengzu_num,aes(x=dir3,y=120,label=paste0("n=",V1)))+
  #geom_jitter()+
  coord_flip()+
  scale_y_continuous(limits = c(15,150),breaks = seq(0,120,10))+
  labs(x="",y="租房均价/m^2/月")

#根据均房价高低排序
zhengzu_summary_mean=zhengzu_summary%>%
  ddply(.,.(dir3),function(x){mean(x$ave_price)})%>%
  arrange(V1)
zhengzu_summary%>%
  mutate(dir3=factor(dir3,levels = zhengzu_summary_mean$dir3))%>%
  ggplot(aes(x=dir3,y=ave_price))+
  geom_boxplot()+
  geom_text(data = zhengzu_num,aes(x=dir3,y=120,label=paste0("n=",V1)))+
  #geom_jitter()+
  coord_flip()+
  scale_y_continuous(limits = c(15,150),breaks = seq(0,120,10))+
  labs(x="",y="租房均价/m^2/月")

#距离地铁站的距离与均价的关系
zhengzu_summary%>%
  filter(!is.na(dir4))%>%
  mutate(dir5=ifelse(dir4<500,"500米以内",ifelse(dir4>=500&dir4<1000,"500-1000米",ifelse(dir4>=1000&dir4<1500,"1000-1500米","大于1500米"))))%>%
  mutate(dir5=factor(dir5,levels=c("500米以内","500-1000米","1000-1500米","大于1500米")))%>%
  ggplot(aes(x=dir5,y=ave_price,color=dir3))+
  geom_boxplot()+
  scale_y_continuous(limits = c(15,150))+
  theme(legend.position = "bottom")+
  labs(x="到1号线站点的距离",y="租房均价/m^2/月")

zhengzu_summary%>%
  ggplot(aes(x=area,y=price,color=dir3))+
  geom_point()+
  scale_x_continuous(limits = c(0,200))+
  scale_y_continuous(limits = c(0,10000))

zhengzu_summary_classmean<-zhengzu_summary%>%
  filter(!is.na(dir4))%>%
  filter(!is.na(dir3))%>%
  mutate(dir5=ifelse(dir4<500,"A",ifelse(dir4>=500&dir4<1000,"B",ifelse(dir4>=1000&dir4<1500,"C","D"))))%>%
  ddply(.,.(dir5,dir3),function(x){
    nrow(x)
  })%>%
  mutate(y=sapply(dir5,function(y){switch(y,"A"=25,"B"=20,"C"=15,"D"=10)}))

zhengzu_summary%>%
  filter(!is.na(dir4))%>%
  filter(!is.na(dir3))%>%
  mutate(dir5=ifelse(dir4<500,"A",ifelse(dir4>=500&dir4<1000,"B",ifelse(dir4>=1000&dir4<1500,"C","D"))))%>%
  ddply(.,.(dir3,dir5),function(x){mean(x$ave_price)})%>%
  ggplot(aes(x=dir3,y=V1,color=dir5))+
  geom_line(aes(color=dir5,group=dir5),size=2)+
  geom_point(aes(color=dir5),size=5)+
  geom_text(data = zhengzu_summary_classmean,aes(x=dir3,y=y,label=V1,color=dir5))+
  #geom_text_repel(aes(label=round(V1,2)),max.overlaps = 100)+
  scale_y_continuous(limits = c(5,90),breaks = seq(0,75,5))+
  scale_color_discrete(label=c("0-500m","500-1000m","1000-1500m","1500m以上"),name="到地铁站距离")+
  theme_prism()+
  theme(axis.text.x = element_text(angle = 45,hjust = 1,vjust = 1),
        legend.title = element_text())+
  #facet_wrap(~factor(dir5),scales = "free_y")+
  labs(y="租房均价/m^2",x="")

相关文章

  • 转发:虞美人·南京地铁二号线

    虞美人·南京地铁二号线 原创 2017-09-27 游戈昊 南京有个号 《江城子·南京地铁一号线》,仍回荡在耳边。...

  • 福田罗湖地铁沿线1房抽样

    福田房源介绍: 芝麻公寓 深圳福田地铁一号线岗厦地铁站D出口,全新精装,家电齐全,阁楼Loft ,拎包入住。 租金...

  • Python学习九十天:突破反爬虫策略

    1.什么是爬虫和反爬虫 爬虫是使用任何技术手段批量获取网站信息的一种方式,反爬虫是使用任何技术手段阻止别人批量获取...

  • 南京地铁是谁的地铁

    这个世界上,最好的老师,是生存。 ——《明朝那些事》 不喜欢南京的地铁一号线,每天...

  • 南京地铁的声音

    如果你也坐过南京地铁一号线,并碰巧也听过这段背景乐。 推荐音乐:Piano Concerto No. 20 in ...

  • 北京二手房分析报告

    北京,作为中国的首都,中国一线大城市,其房产信息备受关注,那么小编就获取了链家网北京二手房源信息,对北京的二手房源...

  • 分布式爬虫scrappy-redis抓取房天下数据

    分布式爬虫scrapy-redis来爬取房天下的各个省份的房源信息(新房和二手房房源信息)newhouse.jso...

  • 房源开发的相关知识和技巧

    房源开发相关知识和技巧 房源吸引客户 优质房源是我们成交的基础 获取房源信息的相关方法和技巧 房源开发的三种形式:...

  • Node爬虫

    使用cheerio爬虫模块抓取页面后获取元素信息跟jQuery基本一样

  • 用 Python 编写一个天气查询应用 pyqt5

    效果预览: ​ 一、获取天气信息 使用python获取天气有两种方式。 ! 1)是通过爬虫的方式获取天气预报网站的...

网友评论

      本文标题:使用爬虫获取南京地铁一号线周边的房源信息并作统计

      本文链接:https://www.haomeiwen.com/subject/saiwhjtx.html