[問題] 爬蟲新手問題請教
[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
使用者(已經有用R 做過不少作品)
但第一次接觸爬蟲
[問題敘述]:
我想爬取 PTT 八卦版的文章,但在用 xpathSApply 函數的時候就會出錯(如下)
Error in UseMethod("xpathApply") :
no applicable method for 'xpathApply' applied to an object of class
"c('xml_document', 'xml_node')"
版上爬完沒有看到類似問題,而google後發現似乎是 httr 套件在 v1.1.0 更新後,
將 XML 改為用 xml2,所以會產生此問題,我試圖降版本但一直安裝失敗,想請問
有沒有別的解法,很抱歉問了這個蠢問題,懇請版上大神幫忙,非常感謝。
[程式範例]:
D <- list()
for( i in 15000:15010){
tmp <- paste(i, '.html', sep='')
url <- paste('www.ptt.cc/bbs/Gossiping/index', tmp, sep='')
html <- content(GET(url,config=set_cookies("over18"="1")))
url.list <- xpathSApply(html, "//div[@class='title']/a[@href]", xmlAttrs)
D <- rbind(D, as.matrix(paste('www.ptt.cc', url.list, sep='')))
}
[環境敘述]:
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese
(Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] magrittr_1.5 rvest_0.3.2 xml2_1.1.0 XML_3.98-1.5
RCurl_1.95-4.8 bitops_1.0-6 httr_1.2.1
[關鍵字]:
選擇性,也許未來有用
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.226.87
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1485153498.A.80D.html
推
01/23 15:19, , 1F
01/23 15:19, 1F
→
01/23 16:52, , 2F
01/23 16:52, 2F
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章