作者bee256 (helloha)
看板R_Language
标题[问题] 爬虫新手问题请教
时间Mon Jan 23 14:38:15 2017
[问题类型]:
程式谘询(我想用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('webptt.com/cn.aspx?n=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('webptt.com/cn.aspx?n=', 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://webptt.com/cn.aspx?n=bbs/R_Language/M.1485153498.A.80D.html
1F:推 clansoda: xml_find_all to replace xpathSapply 01/23 15:19
2F:→ bee256: 成功了,感谢C大 01/23 16:52