ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

R语言爬虫、房价爬取

2021-11-13 23:03:44  阅读:187  来源: 互联网

标签:房价 text 爬虫 爬取 html house nodes inf


install.packages("pacman")  #先安装这个包,方便一键加载其他包
pacman::p_load(XML,rvest,dplyr,stringr)
house_inf <- data.frame()
#爬取前50页
for (i in 1:50) {
  #发现url规律,利用字符串函数进行url拼接并规定编码:
  web <- read_html(str_c("https://cq.lianjia.com/ershoufang/", 82), encoding = "UTF-8")
  #提取房名信息:
  house_name <- web %>% html_nodes(".item a") %>% html_text()
  house_name2 <- ifelse(nchar(house_name) > 8, house_name, NA)
  house_name2 <- na.omit(house_name2)
  #提取房名基本信息并消除空格
  house_basic_inf <- web %>% html_nodes(".houseInfo") %>% html_text()
  house_basic_inf <- str_replace_all(house_basic_inf, " ", "")
  #提取二手房地址
  house_address <- web %>% html_nodes(".positionInfo a") %>% html_text()
  house_address <- str_replace_all(house_address, " ", "")
  house_address <- data.frame(matrix(house_address, ncol = 2, nrow = 30, byrow = T))
  house_qu <- house_address[, 1]
  house_district <- house_address[, 2]

  #提取二手房总价
  house_totalprice <- web %>% html_nodes(".totalPrice") %>% html_text()
  #提取二手房单价
  house_unitprice <- web %>% html_nodes(".unitPrice span") %>% html_text()
  #创建数据框存储以上信息
  house <- data.frame(house_name2, house_basic_inf, house_qu, house_district, house_totalprice, house_unitprice)
  house_inf <- rbind(house_inf, house)
}
length(house_basic_inf)
length(house_name)

#将数据写入csv文档
#去除总价里除了数字以外的其他文字字符
house_inf <- na.omit(house_inf)
price <- house_inf$house_totalprice
price <- stringr::str_remove_all(as.character(price), "[^0-9]")
house_inf$price <- as.numeric(price)
#按房价总价进行排序
house_inf2 <- house_inf[order(house_inf[, 7]),]
#去掉重复的房价信息
house_inf2 <- house_inf2[!duplicated(house_inf2),]
#将结果存为csv格式,路径修改
write.csv(house_inf2, file = "price.csv", row.names = F)
hist(house_inf$price)

注意:如果某个阶段爬取失败,可能是该网址的标签名已更换。需要重新选择标签题目

GitHub下载地址

(R语言爬虫)[https://github.com/lehoso/RWebCrawler]

标签:房价,text,爬虫,爬取,html,house,nodes,inf
来源: https://www.cnblogs.com/lehoso/p/15550177.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有