作者sonic43 (森)
看板R_Language
标题[问题] 591爬虫问题请教
时间Tue May 30 14:22:05 2017
[问题类型]:
程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来)
[软体熟悉度]:
新手(没写过程式,R 是我的第一次)
[问题叙述]:
大家好,我想要爬591这个租屋物件实际的地理位置
https://rent.591.com.tw/rent-detail-5218521.html
也就是 "25.0529842,121.4891891" 这两个数字
其他像是价格、坪数等等都爬得出来,唯读这个爬不出来
请板上大家解答,谢谢
[程式范例]:
library(xml2)
url <- "
https://rent.591.com.tw/rent-detail-5218521.html"
doc <- read_html(url)
xpath <- "//div[@class='propMapBarMap']/iframe"
address <- xml_attrs(xml_find_all(doc, xpath), "src")
#後来研究一下,貌似这个是用iframe(javascript?)生成出来的
#所以这样爬不出来,因此用了phantomjs这招,但还是不成功
#爬出来都没有内容
#phantomjs 参考
https://goo.gl/mf3kd4
#请板上大家帮忙解答了,谢谢
library(rvest)
lines <- readLines("hello.js")
lines[1] <- paste0("var url ='", url ,"';")
writeLines(lines, "hello.js")
system("phantomjs hello.js")
pg <- read_html("1.html")
cid <- pg %>% html_nodes("iframe") %>% html_attr("src")
[环境叙述]:
R version 3.3.1 (2016-06-21)
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
loaded via a namespace (and not attached):
[1] tools_3.3.1
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.115.218.90
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1496125328.A.93C.html
1F:→ celestialgod: 座标是直接跟google API串接的 05/30 17:57
4F:→ sonic43: 你一语道破,我知道怎麽做了,谢谢你! 05/31 00:19