fork(1) download
  1. install.packages("rvest")
  2. install.packages("dplyr")
  3. install.packages("stringr")
  4.  
  5. library(rvest)
  6. library(dplyr)
  7. library(stringr)
  8.  
  9. url = "https://w...content-available-to-author-only...t.nu/kategori.php?k=s426874315&catId=353&sort=price_include_shipping&direction=asc"
  10. price_watch_html <- read_html(url,encoding="UTF-8") %>%
  11. html_nodes(".kGpEgQ")
  12.  
  13. #抓產品名稱
  14. item_name <- price_watch_html %>%
  15. html_attr("aria-label")
  16.  
  17. #抓產品連結
  18. item_url <- price_watch_html %>%
  19. html_attr("href")
  20. item_full_url <- paste0("http://p...content-available-to-author-only...t.nu",item_url)
  21.  
  22. #抓產品價格--> Q1
  23. item_price <- price_watch_html %>%
  24. html_nodes(".bhjqZq") %>%
  25. html_text()
  26. nchar(item_price)
  27.  
  28. #抓每個產品的規格然後對應到 category--> Q2
  29. for (i in 1:length(item_full_url)){
  30. spec <- read_html(item_full_url[i],encoding = "UTF-8") %>%
  31. html_nodes(".iyOoKG") %>%
  32. html_text()
  33. print(spec)
  34. }
  35. print(spec)
  36.  
  37. Category <- c("CPU", "Display", "RAM", "GPU", "Resolution","Weight")
  38.  
  39. #抓產品規格 Table--> Q3
  40. spec_table <- price_watch_html %>%
  41. html_nodes(xpath = '//*[@id="main"]/table') %>%
  42. html_table()
  43. spec_table
  44. class(spec_table)
  45.  
  46.  
  47. #產出一個 13x9 的 data frame
  48. df <- data.frame(Item = item_name, Price = item_price, URL = item_full_url)
  49.  
Success #stdin #stdout #stderr 0.2s 39428KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("rvest") :
  'lib = "/usr/local/lib/R/site-library"' is not writable
Error in install.packages("rvest") : unable to install packages
Execution halted