看板 R_Language 關於我們 聯絡資訊
[問題類型]: 程式諮詢(我想用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://www.ptt.cc/bbs/R_Language/M.1496125328.A.93C.html
celestialgod: 座標是直接跟google API串接的 05/30 17:57
celestialgod: 裡面有一個是https://maps.googleapis.com/maps/api 05/30 17:58
celestialgod: 回傳的json包括的:http://imgur.com/a/c4yOP 05/30 17:59
sonic43: 你一語道破,我知道怎麼做了,謝謝你! 05/31 00:19