-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog_scraping.R
140 lines (115 loc) · 4.76 KB
/
blog_scraping.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#######################################################
#Blog Scraping when URL is organized by date
#########################################################
require(lubridate)
require(plyr)
require(stringr)
require(XML)
require(RCurl)
require(jsonlite)
require(httr)
##################################################################################################
target_links<-url
#####################################################################
#Get_content_post gives the content of a url containing several blog posts. We use the CSS class DIV to delimitate blog posts
#############################################################################
get_content_post <- function (url,selector='post-body entry-content'){
raw <- getURL(url,encoding="UTF-8", ssl.verifypeer = FALSE)
PARSED <- htmlParse(url) #Format the html code d
selector_CSS<- paste( "//div[@class=" , selector , "]" ,sep="")
xpathSApply(PARSED, selector_CSS ,xmlValue)
}
########################################################################
#Get_links returns all the URL links contained in a web page except from "read more" links
########################################################################
getLinks <- function(url,pattern){
raw <- getURL(url,encoding="UTF-8")
PARSED <- htmlParse(raw) #Format the html code d
links<-unique(xpathSApply(PARSED,"//a/@href"))
which<-str_detect(getLinks(url), paste(url,pattern,sep=""))
return (links[which])
}
########################################################################
#GetLinks_readmore returns all the URL links of type "read more" (CSS class <a>) in a given page
########################################################################
getLinks_readmore <- function(url,selector_more="more-link"){
raw <- getURL(url,encoding="UTF-8")
PARSED <- htmlParse(raw) #Format the html code d
selector_CSS<-paste("//a[@class='",selector_more, "']/@href",sep="")
links<-unique(xpathSApply(PARSED,selector_CSS))
return (links)
}
#######################################
##Get_content_api gives the whole content of a page when the page contains only one blog post
#############################################
get_content_api <- function (url){
api_juicer <- 'http://juicer.herokuapp.com/api/article?url='
target <- paste(api_juicer,url,sep="")
raw.data <- readLines(target, warn="F") #en JSON
rd <- fromJSON(raw.data)
return(rd$article$body)
#dat <-data.frame(dat)
}
###############################################
#Create_target_links creates URL links organized by date
##############################################"
create_target_links <- function (url,begin_year,end_year){
target_links<-NULL
for (year in begin_year : end_year){
#From January to September
for (month in 1:9){
target_link<-paste(url,year,paste("0",month,sep=""),"",sep="/")
target_links<-c(target_links,target_link)
}
#From October to December
for (month in 10:12){
target_link<-paste(url,year,month,sep="/")
target_links<-c(target_links,target_link)
}
}
return (target_links)
}
contents<-NULL #A elnver
######################################################
#INPUTS
#################################################
begin_year<-2010
end_year<-2015
url<-"http://www.lescheveuxdemini.com"
######################################################
##Scrap_blog scraps the content of blogs using previous functions
########################################################
scrap_blog <- function(
, url
, direct = TRUE
, read_more = FALSE
, link_pattern = TRUE
, begin_year=2012
, end_year=2015
, pattern_link = "2014"
, selector_post='post-body entry-content'
, selector_more_read="more-link"){
targets <- create_target_links (url,begin_year,end_year)
for (target in targets){
if (read_more){
entire_post_links<-getLinks_readmore(target,selector_more=selector_more_read)
vector_content<- apply(as.matrix(entire_post_links), 1, function(x){content = get_content_api(x)} )
link_date <- rep.int (target, length(entire_post_links) )
df <- as.data.frame (cbind(link_date , vector_content) )
}
if (link_pattern){
entire_post_links<-getLinks(target,pattern=paste(target,pattern_link,sep=""))
vector_content<- apply(as.matrix(entire_post_links), 1, function(x){content = get_content_api(x)} )
link_date <- rep.int (target, length(entire_post_links) )
df <- as.data.frame (cbind(link_date , vector_content) )
}
if (direct){
vector_content<-get_content_post(target,selector=selector_post)
link_date <- rep.int (target, length(entire_post_links) )
df <- as.data.frame (cbind(link_date , vector_content) )
}
else {
df <- data.frame(link_date= character(0)),vector_content =character(0) )
}
} #end of for loop
}#end of scrap_blog function