R_Language 板


LINE

※ 引述《hanglong (小煥)》之銘言: : [問題類型]: : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : 我想要抓台灣每次地震,固定地點的震度 : [軟體熟悉度]: : 新手(沒寫過程式,R 是我的第一次) : [問題敘述]: : 以台灣時間09-11 05:24的地震為例,它的網址是: : https://scweb.cwb.gov.tw/zh-tw/earthquake/details/2019091105245636 : 以此網址為例,我可以擷取固定地點,例如 玉山 的資料 : 但是地震發生很多次,每次的網址都不一樣,不可能每次都手動抓網址, : 再用R抓到玉山的震度。 : 我在地震資料的網址中,可以從原始碼中看到每個地震的連結, : https://scweb.cwb.gov.tw/zh-tw/earthquake/data/ : 因此,如果有辦法可以從這裡擷取到每次地震的網址, : 應該就可以完成我的需求,但是這個網址的部分,不知道該如何擷取, : 在請版上的各位先進幫忙,謝謝。 : [程式範例]: : : 在https://scweb.cwb.gov.tw/zh-tw/earthquake/details/2019091105245636之下 : 利用以下程式,可以看到玉山的震度: : data <- : read_html("https://scweb.cwb.gov.tw/zh-tw/earthquake/details/2019091105245636") : ths <- xml_find_all(data, "//div/ul/li") : xml_text(ths)[substring(xml_text(ths),1,2) == "玉山"] : [1] "玉山 1" : 但在https://scweb.cwb.gov.tw/zh-tw/earthquake/data/之下, : 我想要用一樣的方式,至少先擷取出網址的位置,結果什麼都沒有... : 程式如下: : data <- : read_html("https://scweb.cwb.gov.tw/zh-tw/earthquake/data/") : ths <- xml_find_all(data, "//div/table/tbody/tr/td/a") : xml_text(ths) : character(0) : [環境敘述]: : : [關鍵字]: : 中央氣象局 地震 看原PO好像還沒解決問題... 我來補上我的做法 先上結果圖: https://i.imgur.com/0PWmvmf.png 邏輯解釋: 基本上觀察一下network可以找到相關的id 它便是用ajaxhandler這個API去撈地震資料下來 所以我們可以在network裡面看到這個網址: https://scweb.cwb.gov.tw/zh-tw/earthquake/ajaxhandler 然後Network裡面會跟你說它是用POST,然後打了一個很長的form (下面POST的body) 只要乖乖照著打就可以拿到10筆了 然後只要把length那個參數改掉就能拿更多筆 (九月現在最多25筆) 然後改Search就能切換月份,講完了.... 直接上程式 程式碼: library(httr) library(pipeR) library(xml2) library(stringr) url <- "https://scweb.cwb.gov.tw/zh-tw/earthquake/ajaxhandler" referer_url <- "https://scweb.cwb.gov.tw/zh-tw/earthquake/data/" response_data <- POST(url, body = list("draw" = "1", "columns[0][data]" = "0", "columns[0][name]" = "EventNo", "columns[0][searchable]" = "false", "columns[0][orderable]" = "true", "columns[0][search][value]" = "", "columns[0][search][regex]" = "false", "columns[1][data]" = "1", "columns[1][name]" = "MaxIntensity", "columns[1][searchable]" = "true", "columns[1][orderable]" = "true", "columns[1][search][value]" = "", "columns[1][search][regex]" = "false", "columns[2][data]" = "2", "columns[2][name]" = "OriginTime", "columns[2][searchable]" = "true", "columns[2][orderable]" = "true", "columns[2][search][value]" = "", "columns[2][search][regex]" = "false", "columns[3][data]" = "3", "columns[3][name]" = "MagnitudeValue", "columns[3][searchable]" = "true", "columns[3][orderable]" = "true", "columns[3][search][value]" = "", "columns[3][search][regex]" = "false", "columns[4][data]" = "4", "columns[4][name]" = "Depth", "columns[4][searchable]" = "true", "columns[4][orderable]" = "true", "columns[4][search][value]" = "", "columns[4][search][regex]" = "false", "columns[5][data]" = "5", "columns[5][name]" = "Description", "columns[5][searchable]" = "true", "columns[5][orderable]" = "true", "columns[5][search][value]" = "", "columns[5][search][regex]" = "false", "columns[6][data]" = "6", "columns[6][name]" = "Description", "columns[6][searchable]" = "true", "columns[6][orderable]" = "true", "columns[6][search][value]" = "", "columns[6][search][regex]" = "false", "order[0][column]" = "2", "order[0][dir]" = "desc", "start" = "0", "length" = "10", "search[value]" = "", "search[regex]" = "false", "Search" = "2019年9月", "txtSDate" = "", "txtEDate" = "", "txtSscale" = "", "txtEscale" = "", "txtSdepth" = "", "txtEdepth" = "", "txtLonS" = "", "txtLonE" = "", "txtLatS" = "", "txtLatE" = "", "ddlCity" = "", "ddlCitySta" = "", "txtIntensityB" = "", "txtIntensityE" = "", "txtLon" = "", "txtLat" = "", "txtKM" = "", "ddlStationName" = ""), encode = "form", add_headers(Referer = referer_url)) %>>% content data_ids <- sapply(response_data$data, `[[`, 1L) details_url <- "https://scweb.cwb.gov.tw/zh-tw/earthquake/details/" detail_urls <- paste0(details_url, data_ids) eq_details <- lapply(detail_urls, function(url){ GET(url) %>>% content %>>% xml_find_all("//ul[@class='eqResultBoxRight BulSet BkeyinList']") %>>% xml_find_all("li") %>>% xml_text %>>% str_replace_all("[\\s]", "") %>>% `[`(2L:6L) }) 有任何問題再推文問吧 -- --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 119.14.59.166 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1569512637.A.C38.html







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP