如何使用 R 从多个 "div class" (html) 中提取文本?
How to extract text from a several "div class" (html) using R?
我的目标是从此 html 页面提取信息以创建数据库:
https://drive.google.com/folderview?id=0B0aGd85uKFDyOS1XTTc2QnNjRmc&usp=sharing
变量之一是公寓的价格。我发现有些人有 div class="row_price"
代码,其中包括价格(示例 A),但其他人没有此代码,因此没有价格(示例 B)。因此,我希望 R 可以在没有价格的情况下读取观察结果 NA
,否则它将通过给出随后的观察结果的价格来混合数据库。
示例 A
<div class="listing_column listing_row_price">
<div class="row_price">
$ 14,800
</div>
<div class="row_info">Ayer 19:53</div>
示例 B
<div class="listing_column listing_row_price">
<div class="row_info">Ayer 19:50</div>
我认为如果我提取字符向量中从 "listing_row_price" 到 "row_info" 开头的文本,我将能够得到我想要的输出,即:
...
10 4000
11 14800
12 NA
13 14000
14 8000
...
但到目前为止,我已经用 NA
完成了这个和另一个。
...
10 4000
11 14800
12 14000
13 8000
14 8500
...
使用了命令但没有得到我想要的:
html1<-read_html("file.html")
title<-html_nodes(html1,"div")
html1<-toString(title)
pattern1<-'div class="row_price">([^<]*)<'
title3<-unlist(str_extract_all(title,pattern1))
title3<-title3[c(1:35)]
pattern2<-'>\n\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t([^<*]*)'
title3<-unlist(str_extract(title3,pattern2))
title3<-gsub(">\n\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t $ ","",title3,fixed=TRUE)
title3<-as.data.frame(as.numeric(gsub(",","", title3,fixed=TRUE)))
我也尝试使用 pattern1<-'listing_row_price">([<div class="row_price">]?)([^<]*)<
我认为它说要提取 "listing_row_price" 部分,然后如果存在提取 "row_price" 部分,稍后获取数字并最后提取 <
就是这样。
有很多方法可以做到这一点,根据 HTML 的一致性,一个可能比另一个更好。不过,在这种情况下,一个相当简单的策略是可行的:
library(rvest)
page <- read_html('page.html')
# find all nodes with a class of "listing_row_price"
listings <- html_nodes(page, css = '.listing_row_price')
# for each listing, if it has two children get the text of the first, else return NA
prices <- sapply(listings, function(x){ifelse(length(html_children(x)) == 2,
html_text(html_children(x)[1]),
NA)})
# replace everything that's not a number with nothing, and turn it into an integer
prices <- as.integer(gsub('[^0-9]', '', prices))
我的目标是从此 html 页面提取信息以创建数据库: https://drive.google.com/folderview?id=0B0aGd85uKFDyOS1XTTc2QnNjRmc&usp=sharing
变量之一是公寓的价格。我发现有些人有 div class="row_price"
代码,其中包括价格(示例 A),但其他人没有此代码,因此没有价格(示例 B)。因此,我希望 R 可以在没有价格的情况下读取观察结果 NA
,否则它将通过给出随后的观察结果的价格来混合数据库。
示例 A
<div class="listing_column listing_row_price">
<div class="row_price">
$ 14,800
</div>
<div class="row_info">Ayer 19:53</div>
示例 B
<div class="listing_column listing_row_price">
<div class="row_info">Ayer 19:50</div>
我认为如果我提取字符向量中从 "listing_row_price" 到 "row_info" 开头的文本,我将能够得到我想要的输出,即:
...
10 4000
11 14800
12 NA
13 14000
14 8000
...
但到目前为止,我已经用 NA
完成了这个和另一个。
...
10 4000
11 14800
12 14000
13 8000
14 8500
...
使用了命令但没有得到我想要的:
html1<-read_html("file.html")
title<-html_nodes(html1,"div")
html1<-toString(title)
pattern1<-'div class="row_price">([^<]*)<'
title3<-unlist(str_extract_all(title,pattern1))
title3<-title3[c(1:35)]
pattern2<-'>\n\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t([^<*]*)'
title3<-unlist(str_extract(title3,pattern2))
title3<-gsub(">\n\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t $ ","",title3,fixed=TRUE)
title3<-as.data.frame(as.numeric(gsub(",","", title3,fixed=TRUE)))
我也尝试使用 pattern1<-'listing_row_price">([<div class="row_price">]?)([^<]*)<
我认为它说要提取 "listing_row_price" 部分,然后如果存在提取 "row_price" 部分,稍后获取数字并最后提取 <
就是这样。
有很多方法可以做到这一点,根据 HTML 的一致性,一个可能比另一个更好。不过,在这种情况下,一个相当简单的策略是可行的:
library(rvest)
page <- read_html('page.html')
# find all nodes with a class of "listing_row_price"
listings <- html_nodes(page, css = '.listing_row_price')
# for each listing, if it has two children get the text of the first, else return NA
prices <- sapply(listings, function(x){ifelse(length(html_children(x)) == 2,
html_text(html_children(x)[1]),
NA)})
# replace everything that's not a number with nothing, and turn it into an integer
prices <- as.integer(gsub('[^0-9]', '', prices))