-
Notifications
You must be signed in to change notification settings - Fork 1
/
recuperation_commentaires.R
164 lines (134 loc) · 3.75 KB
/
recuperation_commentaires.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# ---------------------------------------------------------------------------- #
# Script utilisé pour récupérer les commentaires des vidéos tendances
# dont l'url a déjà été stockées dans la bdd de la tendance.
# ---------------------------------------------------------------------------- #
#install.packages("httr")
#install.packages("magrittr")
#install.packages("whisker")
#install.packages("Rtools")
#install.packages("stringr")
#install.packages("RSelenium")
#install.packages("seleniumPipes")
library(httr)
library(jsonlite)
library(xml2)
library(magrittr)
library(whisker)
library(seleniumPipes)
library('RSelenium')
library(stringr)
clicker_bouton <- function(path) {
tryCatch({
button = remDr$findElement('xpath',path )
button$clickElement()
}, error=function(cond) {
print(cond)
return (1)
})
return (0)
}
attendre_si_cap <- function(index=0) {
if (index < 5) {
index <- index + 1
tryCatch ({
remDr$findElement('xpath', "//div[contains(@class, 'captcha')]")
Sys.sleep(5)
attendre_si_cap(index)
}, error=function(cond) {
print("pas de captcha")
print(cond)
})
}
}
scrap <- function(url) {
# url test :
#remDr$navigate("https://www.tiktok.com/@beyondthebrick/video/7255819286801861931?q=lego&t=1701602430679")
remDr$navigate(url)
print("J'attends...\n")
Sys.sleep(5)
resultat <- clicker_bouton("//div[contains(@class, 'DivCloseWrapper')]")
remDr$executeScript("window.scrollBy(0, 1000000);")
print("J'attends...\n")
Sys.sleep(15)
# Attendre si capcha
attendre_si_cap()
print("Commence la récupération")
liste <- ""
tryCatch({
index <- 1
for (com in remDr$findElements('xpath', "//p[contains(@class, 'PCommentText')]/span"))
{
if (com$getElementText() != "") {
print(com$getElementText())
liste <- paste0(liste, ' IC05 ', com$getElementText())
#liste <- append(liste, com$getElementText())
index<- index + 1
}
}
}, error=function(cond) {
print(cond)
#liste<-append(liste, NA)
})
liste_com <<- append(liste_com, liste)
# Essaie de récupérer les hastags
# Avant, il faut peut être cliqué sur le bouton "more" pour afficher le texte
clicker_bouton("//div[contains(@class, 'DivText')]//a/strong")
hash_txt <- ""
tryCatch({
hash_obj <- remDr$findElements(
'xpath',
"//div[contains(@class, 'DivText')]//a/strong"
)
for (hash_o in hash_obj) {
hash_txt <- paste0(hash_txt, hash_o$getElementText())
}
print(paste0("Hash : ", hash_txt))
}, error = function(cond) {
print("can't get hashtags")
print(cond)
})
liste_hashtags <<- append(liste_hashtags, hash_txt)
}
#args <- commandArgs(TRUE)
#if (length(args) == 0) {stop("At least one argument must be supplied")}
remDr <- remoteDriver( port = 4445L, browserName = "firefox", version="1")
remDr$open(silent = TRUE)
#for (index_fichier in c(args[3]:args[4])) {
for (index_fichier in c(4, 8, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20)) {
print(index_fichier)
tryCatch({
bdd <- read.csv(
paste0("./data/videos_trends/v2decouverte-trends-", index_fichier, ".csv"),
sep=";"
)
liste_com <- list()
liste_hashtags <- list()
#for (u in bdd$urls[args[1]:args[2]]) {
for (u in bdd$urls) {
scrap(u)
}
tryCatch({
print("Fini, écriture")
print(liste_com)
print("-----")
print(unlist(liste_com))
bdd$com <- unlist(liste_com)
#bdd$com <- as.character(bdd$com)
print(liste_hashtags)
bdd$hashtags <- unlist(liste_hashtags)
write.table(
x=bdd,
file=paste0("./data/videos_trends/v2decouverte-trends-", index_fichier, "-com.csv"),
sep=";"
, row.names=FALSE
)
}, error=function(cond) {
print("Can't save")
print(cond)
})
}, error = function(cond) {
print("error in process")
print(cond)
})
}
remDr$close()