[問題] 擷取網頁內的某一筆資料
[問題類型]:
程式諮詢(我想用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
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.73.74.11
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1452657983.A.FDA.html
※ 編輯: corel (211.73.74.11), 01/13/2016 14:45:55
※ 編輯: corel (211.73.74.11), 01/13/2016 14:46:30
※ 編輯: corel (211.73.74.11), 01/13/2016 14:47:56
→
01/13 15:13, , 1F
01/13 15:13, 1F
推
01/13 15:26, , 2F
01/13 15:26, 2F
→
01/13 17:08, , 3F
01/13 17:08, 3F
討論串 (同標題文章)
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章