爬取地区房价信息

library(rvest)

从安居客获取济南历城区的二手房房价:(提取top100页)

url = 'https://jinan.anjuke.com/sale/licheng/p3/#filtersort'

编写获取信息函数:在售名称,房价,单价,小区信息

info_fun = function(url){
     web = read_html(url, encoding = 'UTF-8')
     # 获取小区名称:
     house_name = web %>%
          html_nodes('.house-title') %>%
          html_text()
     house_name = gsub(' ','',house_name)
     house_name = gsub('\\n','',house_name)
     # 获取小区房子售价:
     sales_amount = web %>%
          html_nodes('.price-det') %>%
          html_text()
     # 获取单价信息:
     sales_price = web %>%
          html_nodes('.unit-price') %>%
          html_text()
     # 获取小区的地址信息:
     address = web %>%
          html_nodes('.comm-address') %>%
          html_text()
     address = gsub(' ','',address)
     address = gsub('\\n','',address)
     address = gsub('',' ',address)
     result = data.frame(house_name,
                         address,
                         sales_amount,
                         sales_price)
     return(result)
}

将数据保存到all表中
all = data.frame()
for(i in 1:100){
     url = paste('https://jinan.anjuke.com/sale/licheng/p',i,'/#filtersort',sep = '')
     temp = info_fun(url)
     all = rbind(all,temp)
     print(paste('已经采集了',i,'页',sep = ''))
}

 

 

你可能感兴趣的:(数据爬虫)