簡體   English   中英

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)

編輯:基於注釋,修改了代碼。 問題可能出在字符串的編碼上。

#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)

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM