使用Rcurl进行网络垃圾处理
我们想知道2014年1月R-help列表中10张最常见的海报,我使用getURL从ETHZ安全站点检索数据使用Rcurl进行网络垃圾处理,r,regex,rcurl,R,Regex,Rcurl,我们想知道2014年1月R-help列表中10张最常见的海报,我使用getURL从ETHZ安全站点检索数据 检索文件。我们必须使用getURL(),因为模式是https:,否则我们可以使用doc,或者您可以使用rvest(最终使用RCurl)和CSS选择器以更易读的方式执行: library(rvest) jan14 <- html("https://stat.ethz.ch/pipermail/r-help/2009-January/date.html") authors <-
检索文件。我们必须使用
getURL()
,因为模式是https:,否则我们可以使用doc,或者您可以使用rvest
(最终使用RCurl
)和CSS选择器以更易读的方式执行:
library(rvest)
jan14 <- html("https://stat.ethz.ch/pipermail/r-help/2009-January/date.html")
authors <- jan14 %>%
html_nodes("li>i") %>% # CSS selector for <i> after <li>
html_text() %>% # get the text
gsub("\\n", "", .) # remove the newline for each author
tail(sort(table(authors)))
## authors
## Wacek Kusnierczyk jim holtman Duncan Murdoch Prof Brian Ripley
## 55 80 84 84
## David Winsemius Gabor Grothendieck
## 93 116
删除哪些不需要的字符?@johnB我添加了解释文本。非常感谢。你解释得很精彩。。。。最后一个问题:如果不显示频率,我如何列出15张最常见的海报。你能提供代码吗@martin@johnB也许你可以根据上面的代码猜出来?试试你的猜测。@martin table函数将帮助我们提供计数,但由于我们需要列出最频繁的海报,而不需要提及频率,table函数将不会提供help@johnB这是一个命名向量x=c(a=1,b=2)
。研究names()
函数,并使用它提取元素的名称;将你的见解应用到问题上。
1)how can I parse jan14 file using htmltreeparse().
2)how can I use the regular expressions to pull out the author lines and delete unwanted characters in the lines.
url <- "https://stat.ethz.ch/pipermail/r-help/2009-January/date.html"
jan14 <- getURL(url, ssl.verifypeer = FALSE)
doc <- htmlParse(jan14, asText=TRUE)
who <- sapply(doc["//li/i/text()"], xmlValue)
tail(sort(table(sub("[[:space:]]+$", "", who))), 10)
> tail(sort(table(sub("[[:space:]]+$", "", who))), 10)
Greg Snow Henrique Dallazuanna hadley wickham
35 36 40
Marc Schwartz Wacek Kusnierczyk jim holtman
48 55 80
Duncan Murdoch Prof Brian Ripley David Winsemius
84 84 93
Gabor Grothendieck
116
library(rvest)
jan14 <- html("https://stat.ethz.ch/pipermail/r-help/2009-January/date.html")
authors <- jan14 %>%
html_nodes("li>i") %>% # CSS selector for <i> after <li>
html_text() %>% # get the text
gsub("\\n", "", .) # remove the newline for each author
tail(sort(table(authors)))
## authors
## Wacek Kusnierczyk jim holtman Duncan Murdoch Prof Brian Ripley
## 55 80 84 84
## David Winsemius Gabor Grothendieck
## 93 116
library(dplyr)
library(ggplot2)
dat <- data.frame(table(authors)) %>% arrange(-Freq)
gg <- ggplot(dat[1:25,], aes(x=reorder(authors, Freq), y=Freq))
gg <- gg + geom_bar(stat="identity")
gg <- gg + scale_x_discrete(expand=c(0,0))
gg <- gg + scale_y_continuous(expand=c(0,0))
gg <- gg + labs(x=NULL, y="# Posts", title="Top 25 Posters to R-help (Jan 2009)")
gg <- gg + coord_flip()
gg <- gg + theme_bw()
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg