install.packages(“httr”)
install.packages(“jsonlite”)
install.packages(“xml2”)
install.packages(“rvest”)
install.packages(“stringr”)
install.packages(“dplyr”)
install.packages(“ggplot2”)

library(httr)
library(jsonlite)
library(xml2)
library(rvest)
library(ggplot2)

doc <- GET(“https://rent.591.com.tw/index.php?module=search&action=rslist&is_new_list=1&type=1&searchtype=1®ion=0&listview=txt&firstRow=40&totalRows=54040”)

content(doc, “text”)

df <- fromJSON(content(doc, “text”))
View(df)

df[[“main”]]

rent_data <- df[[“main”]]
rent_data

rent_html <- read_html(rent_data)

name <- html_nodes(rent_html, “.address a”)
name <- html_attr(name, “title”)
name

country <- html_nodes(rent_html, “.txt-sh-region”)
country <- html_text(country)
country
town <- html_nodes(rent_html, “.txt-sh-section”)
town <- html_text(town)
town
area <- html_nodes(rent_html, “.area”)
area <- html_text(area)
area

price <- html_nodes(rent_html, “.price .fc-org”)
price <- html_text(price)
price

rent_df <- data.frame(

country = country,
town = town,
name = name,
area = area,
price = price
)

View(rent_df)

library(stringr)

#清理價格資訊,把逗點跟“元“拿掉
rent_df$price <- str_replace_all(rent_df$price,”,|元”,””)
#把文字轉成數字
rent_df$price<- as.numeric(rent_df$price)

View(rent_df)
#建立type欄位,資訊來自area切割後取出後面的部分
rent_df$type<- sapply(str_split(rent_df$area,”/”),”[[“,2)
View(rent_df)

#重整area欄位,用[/]切割area欄位,取出前面的部分作為area欄位
rent_df$area<- sapply(str_split(rent_df$area,”/”),”[[“,1)
#把坪字清除掉
rent_df$area<- str_replace_all(rent_df$area,”坪”,””)
#轉成數字格字
rent_df$area<- as.numeric(rent_df$area)
View(rent_df)

#成立新的unit_price欄位為price欄位除以area欄位
rent_df$unit_price <- rent_df$price / rent_df$area
rent_df$unit_price<- round(rent_df$unit_price)
View(rent_df)

 

library(dplyr)

#dplyr::filter用價格地區等來篩選資料
#把rent591裡面台北市的資料拿出來
rent591_Taipei <- filter(rent_df,country==”台北市”)
View(rent591_Taipei)

#把rent591裡面租金大於一萬的資料拿出來
rent591_Expensive <- filter(rent_df,price>10000)
View(rent591_Expensive)

#把rent591裡面套房資料拿出來
rent591_Tao <- filter(rent_df,type ==”套房”)
View(rent591_Tao)

#dplyr::arrange用價格地區等來排序資料
#把台北市資料以租金由高到低排列

rent591_Taipei_desc <- arrange(rent591_Taipei, desc(price))
View(rent591_Taipei_desc)

#把套房資料以每坪租金由低到高排列

rent591_Tao_price <- arrange(rent591_Tao, unit_price)
View(rent591_Tao_price)

#把套房根據縣市名稱做排序

rent591_Tao_country <- arrange(rent591_Tao, country)
View(rent591_Tao_country )


#dplyr::group_by,用價格地區等來分類資料
#dplyr::summarise,用分類結果彙整資料
#計算台北市各行政區的平均租金
#先把rent591_tp根據行政區分類

rent591_TG <- group_by(rent591_Taipei, town)

#再根據分類結果計算個行政區平均租金
rent591_TGP <- summarise(rent591_TG, mean_price = mean(price))
View(rent591_TGP)

#最後把計算結果排序
rent591_TGP_desc <- arrange(rent591_TGP,desc(mean_price))
View(rent591_TGP_desc)

#計算台北市各行政區的套房平均租金
#先篩選出rent591_tp的套房資料
rent591_Taipei_Tao <- filter(rent591_Taipei, type==”套房”)
View(rent591_Taipei_Tao)

#先篩選出rent591_tp根據行政區分類
rent591_TT_group <- group_by(rent591_Taipei_Tao, town)
View(rent591_TT_group)

#再根據分類結果計算個行政區的平均租金
rent591_TTG_price<- summarise(rent591_TT_group, mean_price = mean(price))
View(rent591_TTG_price)

#最後把計算結果排序
rent591_TTGP <- arrange(
rent591_TTG_price, desc(mean_price)
)
View(rent591_TTGP )

R語言-網路爬蟲與資料清理練習 591租屋網為例
>>將內容用自己的手機、平版分享到.....
標籤:    

One thought on “R語言-網路爬蟲與資料清理練習 591租屋網為例

發佈留言