Re: [問題] 爬蟲抓取資料問題

看板R_Language作者 (天)時間8年前 (2017/03/19 01:38), 8年前編輯推噓1(101)
留言2則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《ya32347844 (虛虛樂)》之銘言: : : : [問題類型]:網路爬蟲 : : : [軟體熟悉度]:入門 : : [問題敘述]: : 我想要抓這個網頁的資料 但不知道是不是Xpath寫錯了 我到後來抓到的資料是NULL : 懇請各位大神給予指教 如果有需要補充的資訊也請不吝指出 : 已經google過相關訊息 用不同的package但結果相同 所以才會覺得是不是一層一層的Tag : 寫錯了 : Update Code: : myheader <- c( : "User-Agent"="Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0_1 like Mac OS X; ja-jp) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A306 Safari/6531.22.7", : "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", : "Accept-Language"="en-us", : "Connection"="keep-alive", : "Accept-Charset"="GB2312,utf-8;q=0.7,*;q=0.7" : ) : #加上myheader : d <- debugGatherer() : get_url <- getURL(url, httpheader = myheader, debugfunction = d$update, verbose = T) : get_url_parse = htmlTreeParse(get_url, encoding = "UTF-8", error=function(...){}, useInternalNodes = TRUE,trim=TRUE) : cat(d$value()[3]) : node<-getNodeSet(get_url_parse, "//div[@class='page-content-wrapper']") : info<-sapply(node,xmlValue) : info : : [程式範例]: : : library(XML) : library(RCurl) : url="https://www.eex.com/en/market-data/environmental-markets/spot-market/european-emission-allowances#!/2017/01/04" : get_url = getURL(url,encoding = "UTF-8") : #將url解析 : get_url_parse = htmlParse(get_url, encoding = "UTF-8") : tablehead <- xpathSApply(get_url_parse, "//div[@id='content']/section[@class='clearfix']/article[@id='market_data']/div[@id='content']/div/div/div/div",xmlValue) : : [環境敘述]: : : mac10.12.2 : : [關鍵字]: : : 先用瀏覽器的開發者工具看一下網頁回傳的資料 會發現它資料是用JSON回傳,也就是說 我們只要抓到那個JSON就可以拿到資料了 => 就不用去parsing HTML了 http://imgur.com/a/ThtWH code: http://pastebin.com/Zt5j8yue 至於你的code應該是少了cookie的setup -- R資料整理套件系列文: magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9 data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.233.137.157 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1489858711.A.903.html ※ 編輯: celestialgod (36.233.137.157), 03/19/2017 01:40:00

03/19 19:14, , 1F
感謝!這麼詳細的解說 其實看出來資料是用Json後 簡單
03/19 19:14, 1F

03/19 19:15, , 2F
很多 果然要抓看看不同網站 才會發現自己缺少哪塊知識
03/19 19:15, 2F
文章代碼(AID): #1OpN2Na3 (R_Language)
討論串 (同標題文章)
文章代碼(AID): #1OpN2Na3 (R_Language)