Saturday, September 19, 2015
A Little Webscraping-Exercise...
In R it's quite easy to pull out anything from a webpage and I'll show a little exercise in doing so. Here I retrieve all blog addresses from R-bloggers by the function readLines() and some subsequent data processing.# get the page's html-code
web_page <- readLines("http://www.r-bloggers.com")
# extract relevant part of web page:
# missing line added on oct. 24th:
ul_tags <- grep("ul>", web_page)
pos_1 <- grep("Contributing Blogs", web_page) + 2
pos_2 <- ul_tags[which(ul_tags > pos_1)[1]] - 2
blog_list_1 <- web_page[pos_1:pos_2]
# extract 2nd element of sublists produced by stringsplit:
blog_list_2 <- unlist(lapply(strsplit(blog_list_1, "\""), "[[", 2))
# exclude elememts without propper address:
blog_list_3 <- blog_list_2[grep("http:", blog_list_2)]
# plot results:
len <- length(blog_list_3)
x <- rep(1:3, ceiling(len/3))[1:len]
y <- 1:len
par(mar = c(0, 5, 0, 5), xpd = T)
plot(x, y, ylab = "", xlab = "", type = "n",
bty = "n", axes = F)
text(x, y, blog_list_3, cex = 0.5)
Labels:
grep(),
HTML,
R,
readLines(),
String-Manipulation,
strsplit(),
sub(),
Web Scraping
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment