R_Language 板


LINE

※ 引述《wanson (望生)》之銘言: : 最近看到有人教學使用RSelenium 抓取PTT的內容 : https://www.youtube.com/watch?v=PYy5C9IIgp8
: 我自學了一下發現的確可行 : 我自己本身因為有搭共乘的需求 : 特別搭乘的是比較少人提供的部分 : 所以我想更進一步使用這個方法並且進一步通知 : 上述網址的教學只能將爬到的檔案存成一個file : 並且限制在該網頁的首頁 : 但是我希望更多的功能為以下,想要請問一下不知道可否使用R更進一步處理 : 1. 爬取更多頁面或是該版所有的頁面 : 這個部分我發現 : 他似乎根據網址的index那邊變動 : 如果我使用最舊就會等於1,下一頁就是二 : 但我使用最新他只會顯示index : https://webptt.com/m.aspx?n=bbs/car-pool/index2.html : 似乎可以寫回圈去提取 : 2. 定期自動爬蟲 : 我想要讓電腦設定每兩小時爬一次,不知道要怎樣設定 : 不知道是否可以教學一下 : 謝謝 : 以下是使用他的教學我產生的code : library(RSelenium) : url= "https://webptt.com/m.aspx?n=bbs/car-pool/index.html" : remDr <- remoteDriver(remoteServerAddr = "localhost" : , port = 4444 : , browserName ="firefox" : ) : remDr$open() #open browser : remDr$getStatus()#check the status of browser : remDr$navigate(url)# website to crawl : #the separate symbol in ppt is r-ent : #get the element from the website : webElem<-remDr$findElements('css selector', ".r-ent")#class for period (.) id : then use # : a = sapply(webElem, function(x){ : c =x$findChildElement('css selector', '.author') : d =x$findChildElement('css selector', '.title') : e =x$findChildElement('css selector', '.date') : cbind(c("author" = c$getElementText(), "title" = : d$getElementText(),e$getElementText())) : } : ) : t=as.data.frame(t(a)) 第一個問題的話就是抓上一頁的按鈕連結,然後再慢慢往前轉就好,下面舉例 RSelenium是滿好入手的工具 抓到感覺後就可以慢慢開始用httr, xml2去抓網頁,速度會相對快很多 舉例如下: (stri_conv只用在windows系統,linux/mac可以不需要) library(httr) library(xml2) library(pipeR) library(stringi) library(stringr) # url url <- "https://webptt.com/m.aspx?n=bbs/car-pool/index.html" # parse網頁 html_nodex <- GET(url) %>>% content # 找轉頁數的按鈕 btnPageChange <- html_nodex %>>% xml_find_all("//div/a[@class='btn wide']") # 找上一頁按鈕的位置 locPrevPage <- btnPageChange %>>% xml_text %>>% stri_conv("UTF8", "BIG5") %>% str_detect("上頁") # 看上一頁的index編號 indexCount <- btnPageChange[locPrevPage] %>>% xml_attr("href") %>>% str_match("index(\\d{4})") %>>% `[`(2) %>>% as.integer # 要抓的欄位資訊 infoVec <- c(title = "//div[@class='title']", date = "//div[@class='date']", author = "//div[@class='author']") # 先抓第一頁的 info <- sapply(infoVec, function(xpath){ xml_find_all(html_nodex, xpath) %>>% xml_text %>>% stri_conv("UTF8", "BIG5") %>>% str_replace_all("\t|\n| ", "") }) # 把前天的日期抓出來 (只要date出現前天,就停止, # 當然有可能置底是前天發的就GG了,這個再自己修改) crawlDate <- format(Sys.Date()-2, "%m/%d") %>>% str_extract("[1-9]{1,2}/\\d{2}") while (continueCrawl) { # 把index.html改成上一頁/上上一頁/... html_nodex <- url %>>% str_replace("index.html", sprintf("index%i.html", indexCount)) %>>% GET %>>% content # 抓出那一頁的資訊 tmpInfo <- sapply(infoVec, function(xpath){ xml_find_all(html_nodex, xpath) %>>% xml_text %>>% stri_conv("UTF8", "BIG5") %>>% str_replace_all("\t|\n| ", "") }) # 跟之前的合併 info <- rbind(info, tmpInfo) # 如果出現不是昨天日期的就停止 if (any(tmpInfo[ , "date"] == crawlDate)) continueCrawl <- FALSE indexCount <- indexCount - 1 } info # 即為所求 每兩小時執行一次的話,就外面加一層while (TRUE)然後搭配Sys.sleep使用 -- R資料整理套件系列文: magrittr #1LhSWhpH (R_Language) http://tinyurl.com/j3ql84c data.table #1LhW7Tvj (R_Language) http://tinyurl.com/hr77hrn dplyr(上) #1LhpJCfB (R_Language) http://tinyurl.com/jtg4hau dplyr(下) #1Lhw8b-s (R_Language) tidyr #1Liqls1R (R_Language) http://tinyurl.com/jq3o2g3 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.246.29.181
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1469354217.A.46B.html ※ 編輯: celestialgod (111.246.29.181), 07/24/2016 18:11:18







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP