看板 R_Language 關於我們 聯絡資訊
[軟體熟悉度]: 使用者(已經有用R 做過不少作品) [問題敘述]: 資料庫網站本身載入就不快,有時更會塞車,讀取網站的檔案或路徑時, 回傳讀取的連結(檔案或路徑)list,即便網站路徑存在還是會讀不到。 網站讀取如果快的時候可以完成,請見範例程式。 如果慢的時候,就算連結存在,會出現錯誤,當下我開網頁大約需要4分鐘。 程式約3秒出現錯誤訊息。錯誤訊息為: Error in open.connection(x, "rb") : cannot open the connection。 因為希望程式能自動每日執行,不會卡在有時因網站loading慢而中止,有甚麼方式 可以讓程式等待網站呢? [程式範例]: 成功的: "http://tisvcloud.freeway.gov.tw/history/" %>>% (~ cat('Now the program is at ', ., "\n")) %>>% read_html("UTF8") %>>% xml_find_all("//*[@id='form1']//table/tbody") %>>% xml_find_all("//tr/td[1]/a") %>>% xml_text %>>% when(identical(.Platform$OS.type, "windows") ~ stri_conv(., "UTF-8", "Big5"), ~ .) 結果: Now the program is at http://tisvcloud.freeway.gov.tw/history/ [1] "Parent Directory" "vd" "TDCS_PINGLIN" "TDCS" [5] "roadlevel" "motc20" "map" ... [25] "1min_incident_data_1968.xml" "1968_incident.pdf" -- 失敗的: "http://tisvcloud.freeway.gov.tw/history/TDCS/M08A/" %>>% (~ cat('Now the program is at ', ., "\n")) %>>% read_html("UTF8") %>>% xml_find_all("//*[@id='form1']//table/tbody") %>>% xml_find_all("//tr/td[1]/a") %>>% xml_text %>>% when(identical(.Platform$OS.type, "windows") ~ stri_conv(., "UTF-8", "Big5"), ~ .) 結果: Now the program is at http://tisvcloud.freeway.gov.tw/history/TDCS/M08A/ Error in open.connection(x, "rb") : cannot open the connection [環境敘述]: WIN 10 R 4.0.4 [關鍵字]: 爬蟲;網站壅塞 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 124.109.113.253 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1616150204.A.551.html
andrew43: read_html(..., options = "RECOVER") 有用嗎? 03/19 22:51
CruxLaelaps: 不行,結果一樣 03/20 00:18
showfeb: 用try(...)包住語法, 迴圈遇錯重覆執行. 03/20 00:48
我貼上的程式本來就被try()包住,又加上repeat包住,重複做,只是結果會有錯誤訊息的無窮迴圈...網路實在太卡 --
locka: 把read_html的結果存成一個變數,用if判斷有抓到東西才做後 03/20 01:21
locka: 續解析 03/20 01:21
cywhale: 如果是這樣的話,用 future(read_html) 再解析value()? 03/20 08:35
celestialgod: https://reurl.cc/1g83D8 use rvest + timeouts 03/20 14:32
是用這位學長提的的方法解決,套件"httr"內的函數GET()設timeout 感謝這位學長! --
celestialgod: 或是三樓建議那樣 可能比較好 03/20 14:32
celestialgod: 這段code看起來是我5年前為某老師寫的... 03/20 14:33
celestialgod: https://imgur.com/4d7yC60 03/20 14:34
celestialgod: 沒想到會在R板看到自己寫的東西XDDD 03/20 14:34
※ 編輯: CruxLaelaps (124.109.113.253 臺灣), 03/25/2021 15:59:46