看板 R_Language 關於我們 聯絡資訊
我覺得在這個網站應該直接找包括 "Human uses" 內容的 h1 為起點會比較可靠。 找到後,取出它之後的第一個同級 div,再取其之下的 span。 如果只是靠「找到第n個class為xxx的div」, 特別該class只是拿來套用樣式而沒有結構上的意義, 那你換另一個頁面時這招大概又沒用了(第幾個class又要重找)。 html_nodes( read_html("http://www.fishbase.org/summary/Decapterus-maruadsi.html"), xpath = "//h1[contains(text(),'Human uses')]/following-sibling::div[1]/span" ) %>% html_text %>% trimws ※ 引述《mary84215 (過剩芒果蘇)》之銘言: : [問題類型]: : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : [軟體熟悉度]: : 使用者(已經有用R 做過不少作品) : [問題敘述]: : 各位大大好,我想在FishBase網站把這種魚的Human uses爬下来,網址如下方所列 : http://www.fishbase.org/summary/Decapterus-maruadsi.html : 以此網頁為例,我使用套件rvest爬蟲,並配合SelectorGadget取得元素的css路徑或是xp : ath : 原本期待能爬出"Fisheries: highly commercial",但是不知爲何總是爬出character(0) : 已經試過許多方法,且此網頁應該也不是動態網頁,但還是不得其解QQ : 只好來這裡求助各位大大了,謝謝各位 : [程式範例]: : rm(list=ls()) : library("rvest") : library("xml2") : library("magrittr") : library("httr") : page=read_html("http://www.fishbase.org/summary/Decapterus-maruadsi.html") : page %>% html_nodes(".smallSpace:nth-child(44) span") %>% : html_text() : [環境敘述]: : R version 3.3.1 (2016-06-21) : Platform: x86_64-apple-darwin13.4.0 (64-bit) : Running under: OS X 10.12.6 (Sierra) : locale: : [1] zh_TW.UTF-8/zh_TW.UTF-8/zh_TW.UTF-8/C/zh_TW.UTF-8/zh_TW.UTF-8 : attached base packages: : [1] stats graphics grDevices utils datasets methods base : other attached packages: : [1] httr_1.3.1 magrittr_1.5 rvest_0.3.2 xml2_0.1.2 : loaded via a namespace (and not attached): : [1] selectr_0.4-1 R6_2.1.2 tools_3.3.1 curl_3.1 Rcpp_0.12.4 : [6] stringi_1.0-1 stringr_1.0.0 : [關鍵字]: : 爬蟲、rvest -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.222.1 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1540342744.A.AF5.html
cywhale: 推先找Human uses 10/24 09:35
mary84215: 了解,感謝大大們解答! 10/24 23:54