R_Language 板


LINE

※ 引述《wanson (望生)》之铭言: : 最近看到有人教学使用RSelenium 抓取PTT的内容 : https://www.youtube.com/watch?v=PYy5C9IIgp8
: 我自学了一下发现的确可行 : 我自己本身因为有搭共乘的需求 : 特别搭乘的是比较少人提供的部分 : 所以我想更进一步使用这个方法并且进一步通知 : 上述网址的教学只能将爬到的档案存成一个file : 并且限制在该网页的首页 : 但是我希望更多的功能为以下,想要请问一下不知道可否使用R更进一步处理 : 1. 爬取更多页面或是该版所有的页面 : 这个部分我发现 : 他似乎根据网址的index那边变动 : 如果我使用最旧就会等於1,下一页就是二 : 但我使用最新他只会显示index : https://webptt.com/cn.aspx?n=bbs/car-pool/index2.html : 似乎可以写回圈去提取 : 2. 定期自动爬虫 : 我想要让电脑设定每两小时爬一次,不知道要怎样设定 : 不知道是否可以教学一下 : 谢谢 : 以下是使用他的教学我产生的code : library(RSelenium) : url= "https://webptt.com/cn.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/cn.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/cn.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