R- gsub 不替换文本
R- gsub Doesn't Replace Text
恐怕这是我第一次去 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)
EDIT: Based on comments, modified the code. The issue probably lies in
the encodig of the string.
#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)
希望对您有所帮助!
恐怕这是我第一次去 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)
EDIT: Based on comments, modified the code. The issue probably lies in the encodig of the string.
#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)
希望对您有所帮助!