作者celestialgod (天)
看板R_Language
標題Re: [問題] 擷取網頁內的某一筆資料
時間Wed Jan 13 17:17:31 2016
※ 引述《corel (可羅)》之銘言:
: [問題類型]:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
: [軟體熟悉度]:
: 入門(寫過其他程式,只是對語法不熟悉)
: [問題敘述]:
: 請簡略描述你所要做的事情,或是這個程式的目的
: # Q:想要擷取出網頁中某區塊標籤所有的文字,包含html標籤等內的某一資料
: #結果:
: #擷取出網頁內所有同屬性的資料...
: # crawl library ----
: library(bitops)
: library(XML)
: library(RCurl)
: # only for windows -----
: signatures <- system.file("CurlSSL", cainfo="cacert.pem", package="RCurl")
: # 網址縮址 http://tinyurl.com/jgoun9c
: home <-
: "http://www.expodatabase.com/aussteller/messen/index.php?OK=1&sortierid=0&maxPerPage=20&i_cockpitkeyfindwo=2&i_cockpitkeyfindart=1&currPage=1"
: home <- getURL(home, cainfo = signatures)
: home <- htmlParse(home)
: # 共有20筆的block
: block <-getNodeSet(home, "//div[@class='shm']")
: # length(block) # 共20筆
: doc <- block[[1]] # 抓取第一筆
: # doc
: # <div class="shm">
: # <div class="listdates">
: # <div class="date">08Jan-17Jan2016</div>
: # </div>
: # <div class="search_result_list_box">
: # <div class="city">London, United Kingdom</div>
: # <div class="firma"><a
: href="show.php?id=352&timer=m1452657261&tmid=&currPage=1&maxPerPage=20&params=timer%3Dm1452657261%26amp%3Btimer%3Dm1452657261%26amp%3Bi_cockpitkeyfindwo%3D2%26amp%3Bi_cockpitkeyfindart%3D1%26amp%3Bsortierid%3D0%26amp%3Btimer%3Dm1452657261%26amp%3BmaxPerPage%3D20%26amp%3BshowPrintlist%3D0%26amp%3BmaxPerPage%3D20">London
: Boat Show</a></div>
: # </div>
: # <div class="search_result_box_right">
: # <div class="branchen"><strong>Business sectors:</strong> Boats</div>
: # </div>
: # <div class="fixfloat"></div>
: # </div>
: # 想要抓取 block[[1]]筆的 class="date" 的 08Jan-17Jan2016
: date <- xpathSApply(doc, "//div[@class='date']", xmlValue)
: # 結果秀出網頁內全部 date 的資料
: # [1] "08Jan-17Jan2016" "08Jan-17Jan2016" "09Jan-17Jan2016" "07Jan-15Jan2017"
: "06Jan-14Jan2018"
: # [6] "09Jan-17Jan2016" "09Jan-17Jan2016" "09Jan-17Jan2016" "09Jan-17Jan2016"
: "10Jan-13Jan2016"
: # [11] "10Jan-13Jan2016" "10Jan-13Jan2016" "10Jan-13Jan2016"
: "11Jan-13Jan2016" "11Jan-13Jan2016"
: # [16] "11Jan-13Jan2016" "11Jan-14Jan2016" "11Jan-14Jan2016"
: "11Jan-14Jan2016" "11Jan-14Jan2016"
: # [21] "11Jan-14Jan2016" "11Jan-24Jan2016" "09Jan-22Jan2017"
: 我的問題是, doc <- block[[1]]
: 不是己經抓取出 於 block 中第一筆資料並且儲存在doc中嗎?
: 結果用 xpathSpply 擷出來的..竟然是網頁中 date 全部資料?
: 想請教一下, 我該用那一個函數或方法才能只抓到 block[[1]]中 xml的資料?
: 並且解析出 date 為 08Jan-17Jan2016 ?
: 謝謝
: #線上程式:
: # http://ideone.com/ZqHAqr
個人經驗,XML需要比較多的記憶體又比較慢,還有可能有memory leak的問題
我推薦你用xml2這個套件XD
library(xml2)
library(magrittr)
library(RCurl)
home <-
"
http://www.expodatabase.com/aussteller/messen/index.php?OK=1&sortierid=0&
maxPerPage=20&currPage=1"
signatures <- system.file("CurlSSL", cainfo="cacert.pem", package="RCurl")
html_page <- getURL(home, cainfo = signatures)
read_html(html_page) %>% xml_find_all(".//div[@class='shm']") %>% .[[1]] %>%
xml_find_all(".//div[@class='date']") %>% xml_text
# [1] "08Jan-17Jan2016"
--
R資料整理套件系列文:
magrittr #1LhSWhpH (R_Language) http://tinyurl.com/1LhSWhpH
data.table #1LhW7Tvj (R_Language) http://tinyurl.com/1LhW7Tvj
dplyr(上) #1LhpJCfB (R_Language) http://tinyurl.com/1LhpJCfB
dplyr(下) #1Lhw8b-s (R_Language)
tidyr #1Liqls1R (R_Language) http://tinyurl.com/1Liqls1R
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.109.74.87
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1452676654.A.3E6.html
→ Wush978: 請問有對XML批評的出處嗎?純追根究底 01/14 07:14
上面就寫個人經驗啦XDD 我自己試的結果
之前parse了上萬個xml,發現XML真的沒那麼好用QQ
xml2比較方便XD
stackoverflow隨便一搜,其實滿多人有在講XML有memory leak的問題..
推 corel: PUSH 01/15 07:45
※ 編輯: celestialgod (140.109.74.87), 01/15/2016 11:41:35