恐怕这是我第一次去R和刮,所以请容忍我。
我试图从一个网站的价格数据,似乎无法清除的非必要的字符,只留下数字。
任何建议都很受欢迎!
#Specifying the url for the website
url <- 'https://www.immobilienscout24.de/Suche/S-4/Wohnung-Kauf/Berlin/Berlin/-/1,00-'
#Reading the HTML code from the website
webpage <- read_html(url)
#Using CSS selectors to scrap the rankings section
price_data_html <- html_nodes(webpage,'.result-list-entry__primary-criterion:nth-child(1)')
#Converting the ranking data to text
price_data <- html_text(price_data_html)
#Data-Preprocessing: removing non-numbers
price_data<-gsub("\n","",price_data)
price_data<-gsub(" € Kaufpreis ",
"",price_data)
price_data<-gsub(" ","",price_data)
price_data<-gsub(" €Kaufpreis ","",price_data)
#Reviewing the data
head(price_data)发布于 2017-07-16 10:23:24
编辑:基于注释,修改代码。问题可能在于字符串的排列。
#Data-Preprocessing: removing non-numbers
price_data<-gsub("\n","",price_data)
price_data<-gsub("Kaufpreis","",price_data)
price_data<-gsub(" ","",price_data)
price_data = gsub("[^[:alnum:].]", "", price_data)希望这能有所帮助!
https://stackoverflow.com/questions/45126989
复制相似问题