-
Notifications
You must be signed in to change notification settings - Fork 0
/
trying to scrape images for ds.R
136 lines (100 loc) · 3.62 KB
/
trying to scrape images for ds.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
library(tidyverse)
library(lubridate)
library(stringr)
library(tidyr)
library(tidylog)
library(rvest)
library(googleAnalyticsR)
library(googleAuthR)
options(scipen=10000)
gar_auth("tu.httr-oauth")
account_list <- ga_account_list()
meta <- ga_meta()
mac_id <- 142253091
as.Date(start <- "2019-10-01")
as.Date(end <- "2019-10-02")
mac <- google_analytics(mac_id, date_range = c(start, end),
dimensions = c("productName","pagePath"),
metrics = c("cartToDetailRate", "itemRevenue"),
max = -1,
anti_sample = TRUE)
glimpse(mac)
mac2 <- mac %>% group_by(pagePath) %>% summarize(n=n_distinct(pagePath))
mac3 <- mac2 %>% filter(str_detect(pagePath, regex(".*/product/.*", ignore_case = TRUE)))
mac3$n <- NULL
glimpse(mac3)
mac4 <- mac3 %>% mutate(hostname = "https://www.maccosmetics.co.il", full_url= paste0(hostname,pagePath))
mac5 <- mac4 %>% select(full_url)
Catcher1 <- data.frame(P_URL=character(), image=character())
URLs <- mac5$full_url
for(i in URLs) {
WS1 <- read_html(i)
image <- WS1 %>%
html_nodes(xpath = '//meta[@property="og:image"]') %>%
html_attr('content')
P_URL <- paste0(i)
if(length(image)>0){
temp <- data.frame(P_URL, image)
Catcher1 <- rbind(Catcher1,temp)
}
else{
image <- "No Image - Check the link"
temp <- data.frame(P_URL, image)
Catcher1 <- rbind(Catcher1,temp)
}
cat("*")
}
image_df <- Catcher1
names <- c("product_name", "image_url")
names(image_df) <- names
table1 <- google_analytics(mac_id, date_range = c(start, end),
dimensions = c("productName"),
metrics = c("itemQuantity", "itemRevenue"),
max = -1,
anti_sample = TRUE)
table2 <- image_df
table3 <- mac %>% filter(str_detect(pagePath, regex(".*/product/.*", ignore_case = TRUE)))
o <- left_join(table1, table3, by="productName")
table3 <- table3 %>% rename(P_URL = pagePath)
table2 <- table2 %>% rename(P_URL = product_name)
table2$P_URL <- table2$P_URL %>% str_replace_all("https://www.maccosmetics.co.il","")
joined_2_3 <- left_join(table3, table2, by="P_URL")
joined_2_3 <- joined_2_3 %>% select(productName, P_URL, image_url)
final <- left_join(table1, joined_2_3, by="productName")
#############################
## Uploading to GCS and BQ ##
#############################
#-- Setting up BigQuery and GCS --#
library(googleCloudStorageR)
library(bigQueryR)
#-- Setting Project --#
bqr_global_project("erez-bigquery")
bqr_get_global_project()
#-- get project list --#
projects <- bqr_list_projects()
head(projects)
my_project <- projects[2,1]
my_project_id <- projects[2,3]
#-- for first project, get datasets --#
datasets <- bqr_list_datasets(my_project)
head(datasets)
my_dataset <- datasets[1]
head(my_dataset)
#####################
#### Upload Data ####
#####################
#-- Get Buckets --#
gcs_list_buckets("stellar-vista-149518")
gcs_list_buckets("erez-bigquery")
#-- Set bucket to store the data --#
gcs_global_bucket("erez-bigquery-storage-bucket")
###########################
#### GCS Upload Data ####
##########################
#-- Upload To Storage --#
f <- function(input, output) {
write.table(input, sep = ",", col.names = TRUE, row.names = FALSE,
quote = FALSE, file = output, qmethod = "double")}
filename=paste0("mac_product_images.csv")
gcs_upload(final, name = filename, object_function = f)
#gcs_upload(df, name = "easydf.csv", object_function = f)