利用文字爬蟲獲取pchome 上的Iphone 7 價格 並匯出成EXCEL

suppressWarnings(library(RCurl))
## Loading required package: bitops
suppressWarnings(library("mailR"))

suppressWarnings(library(stringr))
suppressWarnings(library(jsonlite))
suppressWarnings(library(xlsxjars))
## Loading required package: rJava
## 
## Attaching package: 'rJava'
## The following object is masked from 'package:RCurl':
## 
##     clone
suppressWarnings(library(xlsx))
#Pchome 上的價格並不是以HTML 呈現, 其價格是透過JSON URL
#利用lapply 來讀15頁的商品
firsturl <- "http://ecshweb.pchome.com.tw/search/v3.3/all/results?q=Apple&page="
lasturl <- "&sort=rnk/dc"
totalurl<-do.call(
    rbind,
    lapply(
        1:15, 
        function(i) paste(firsturl,i,lasturl,sep = "")))

apple_raw<-do.call(rbind,lapply(totalurl[,1], function(x) fromJSON(x, simplifyDataFrame = TRUE))) 
#從Large List 轉變為 DataFrame

apple_price <- do.call(rbind, lapply(1:15,function(i) data.frame(product= unlist(apple_raw[i,]$prods$name),price = unlist(apple_raw[i,]$prods$price),stringsAsFactors = FALSE)))
apple_price<-apple_price[!duplicated(apple_price),]
apple_price$time <- format(Sys.time(), "%H:%M:%S")
#抓取Iphone7 Plus 價格
Iphone7_price<- apple_price[str_detect(apple_price$product,"iPhone 7 Plus") == TRUE,]
Iphone7_price
##                                                                                 product
## 2                                                     Apple iPhone 7 Plus (128G)-曜石黑
## 5                                                            Apple iPhone 7 Plus (128G)
## 7                                                             Apple iPhone 7 Plus (32G)
## 22                                                           Apple iPhone 7 Plus (128G)
## 58                                                  [福利品] Apple iPhone 7 Plus (128G)
## 65                                                     Apple iPhone 7 Plus 128G 福利品 
## 84                                                    Apple iPhone 7 Plus (256G)-曜石黑
## 86                                                            Apple iPhone 7 Plus (32G)
## 107                                       [原廠公司貨] APPLE 原廠 iPhone 7 Plus矽膠護套
## 118                                                          Apple iPhone 7 Plus (128G)
## 141                                                          Apple iPhone 7 Plus (128G)
## 144                                                    Apple iPhone 7 Plus (32G) 福利品
## 145                                                   Apple iPhone 7 Plus (128G)-曜石黑
## 166                                      [原廠公司貨] APPLE 原廠 iPhone 7 Plus 皮革護套
## 169                                   iMOS Apple iPhone 7 Plus 3SAS 疏油疏水 背面保護貼
## 173                                                    Apple iPhone 7 Plus (128G)曜石黑
## 174                     iMOS Apple iPhone 7 Plus 5.5吋 9H康寧強化玻璃螢幕保護貼(非滿版)
## 197 iMOS Apple iPhone 7 Plus (黑邊) 3D曲面9H強化滿版玻璃螢幕保護貼+不鏽鋼金屬環(曜石黑)
## 198                                   UCASE Apple iPhone 7 Plus(5.5吋)360度全包覆保護殼
## 199                        STORYLEATHER APPLE iPhone 7 Plus (5.5吋) 手機殼 小牛皮黑現貨
## 200                                                   Apple iPhone 7 Plus (256G)-消光黑
## 212                                      Apple iPhone 7 Plus 專用側翻式鋼板皮套 MOFI 睿
## 215                                                    Apple iPhone 7 Plus 32G 福利品  
## 229                   COLORS Apple iPhone 7 Plus / i7+ 5.5吋 神秘跳動指環手機殼(送掛繩)
## 297                    Apple iPhone 7 Plus 5.5吋 強化玻璃保護貼-黑色-2片裝-{滿版3D曲面}
##     price     time
## 2   31800 21:50:20
## 5   31800 21:50:20
## 7   27699 21:50:20
## 22  31999 21:50:20
## 58  29990 21:50:20
## 65  29990 21:50:20
## 84  36900 21:50:20
## 86  28500 21:50:20
## 107  1390 21:50:20
## 118 32900 21:50:20
## 141 32290 21:50:20
## 144 26990 21:50:20
## 145 32290 21:50:20
## 166  1690 21:50:20
## 169   600 21:50:20
## 173 32900 21:50:20
## 174   990 21:50:20
## 197  1690 21:50:20
## 198   390 21:50:20
## 199  1280 21:50:20
## 200 36900 21:50:20
## 212   350 21:50:20
## 215 26590 21:50:20
## 229   399 21:50:20
## 297   499 21:50:20
setwd("C:/Users/dannyhuang/Desktop/")
#抓到的價格資料自動寄到指定信箱
write.csv(Iphone7_price,file= "C:/Users/dannyhuang/Desktop/Iphone7_pchome.csv",row.names=FALSE) #匯出成EXCEL
send.mail(from = "hathathat8976@gmail.com",
          to = c("nk801230@gmail.com"),
          subject = "Iphone7 Plus Deal Alert",
          body <- "Hello, my name is Danny",
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "hathathat8976@gmail.com", passwd = "home1234", ssl = TRUE),
          encoding = "utf-8",
          authenticate = TRUE,
          attach.files= c("C:/Users/dannyhuang/Desktop/Iphone7_pchome.csv"),
          send = TRUE)
## [1] "Java-Object{org.apache.commons.mail.MultiPartEmail@43195e57}"
read.csv("Iphone7_pchome.csv")#讀取該檔案
##                                                                                product
## 1                                                    Apple iPhone 7 Plus (128G)-曜石黑
## 2                                                           Apple iPhone 7 Plus (128G)
## 3                                                            Apple iPhone 7 Plus (32G)
## 4                                                           Apple iPhone 7 Plus (128G)
## 5                                                  [福利品] Apple iPhone 7 Plus (128G)
## 6                                                     Apple iPhone 7 Plus 128G 福利品 
## 7                                                    Apple iPhone 7 Plus (256G)-曜石黑
## 8                                                            Apple iPhone 7 Plus (32G)
## 9                                        [原廠公司貨] APPLE 原廠 iPhone 7 Plus矽膠護套
## 10                                                          Apple iPhone 7 Plus (128G)
## 11                                                          Apple iPhone 7 Plus (128G)
## 12                                                    Apple iPhone 7 Plus (32G) 福利品
## 13                                                   Apple iPhone 7 Plus (128G)-曜石黑
## 14                                      [原廠公司貨] APPLE 原廠 iPhone 7 Plus 皮革護套
## 15                                   iMOS Apple iPhone 7 Plus 3SAS 疏油疏水 背面保護貼
## 16                                                    Apple iPhone 7 Plus (128G)曜石黑
## 17                     iMOS Apple iPhone 7 Plus 5.5吋 9H康寧強化玻璃螢幕保護貼(非滿版)
## 18 iMOS Apple iPhone 7 Plus (黑邊) 3D曲面9H強化滿版玻璃螢幕保護貼+不鏽鋼金屬環(曜石黑)
## 19                                   UCASE Apple iPhone 7 Plus(5.5吋)360度全包覆保護殼
## 20                        STORYLEATHER APPLE iPhone 7 Plus (5.5吋) 手機殼 小牛皮黑現貨
## 21                                                   Apple iPhone 7 Plus (256G)-消光黑
## 22                                      Apple iPhone 7 Plus 專用側翻式鋼板皮套 MOFI 睿
## 23                                                    Apple iPhone 7 Plus 32G 福利品  
## 24                   COLORS Apple iPhone 7 Plus / i7+ 5.5吋 神秘跳動指環手機殼(送掛繩)
## 25                    Apple iPhone 7 Plus 5.5吋 強化玻璃保護貼-黑色-2片裝-{滿版3D曲面}
##    price     time
## 1  31800 21:50:20
## 2  31800 21:50:20
## 3  27699 21:50:20
## 4  31999 21:50:20
## 5  29990 21:50:20
## 6  29990 21:50:20
## 7  36900 21:50:20
## 8  28500 21:50:20
## 9   1390 21:50:20
## 10 32900 21:50:20
## 11 32290 21:50:20
## 12 26990 21:50:20
## 13 32290 21:50:20
## 14  1690 21:50:20
## 15   600 21:50:20
## 16 32900 21:50:20
## 17   990 21:50:20
## 18  1690 21:50:20
## 19   390 21:50:20
## 20  1280 21:50:20
## 21 36900 21:50:20
## 22   350 21:50:20
## 23 26590 21:50:20
## 24   399 21:50:20
## 25   499 21:50:20