首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R- gsub不能代替文本。

R- gsub不能代替文本。
EN

Stack Overflow用户
提问于 2017-07-16 09:21:24
回答 1查看 369关注 0票数 0

恐怕这是我第一次去R和刮,所以请容忍我。

我试图从一个网站的价格数据,似乎无法清除的非必要的字符,只留下数字。

任何建议都很受欢迎!

代码语言:javascript
复制
#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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-16 10:23:24

编辑:基于注释,修改代码。问题可能在于字符串的排列。

代码语言:javascript
复制
#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)

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45126989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档