[問題] 爬蟲,因為壅塞想讓程式等待

看板R_Language作者 (唷咪)時間3年前 (2021/03/19 18:36), 3年前編輯推噓0(004)
留言4則, 1人參與, 3年前最新討論串1/1
[軟體熟悉度]: 使用者(已經有用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

03/19 22:51, 3年前 , 1F
read_html(..., options = "RECOVER") 有用嗎?
03/19 22:51, 1F

03/20 00:18, 3年前 , 2F
不行,結果一樣
03/20 00:18, 2F

03/20 00:48, 3年前 , 3F
用try(...)包住語法, 迴圈遇錯重覆執行.
03/20 00:48, 3F
我貼上的程式本來就被try()包住,又加上repeat包住,重複做,只是結果會有錯誤訊息的無窮迴圈...網路實在太卡 --

03/20 01:21, 3年前 , 4F
把read_html的結果存成一個變數,用if判斷有抓到東西才做後
03/20 01:21, 4F

03/20 01:21, 3年前 , 5F
續解析
03/20 01:21, 5F

03/20 08:35, 3年前 , 6F
如果是這樣的話,用 future(read_html) 再解析value()?
03/20 08:35, 6F

03/20 14:32, 3年前 , 7F
https://reurl.cc/1g83D8 use rvest + timeouts
03/20 14:32, 7F
是用這位學長提的的方法解決,套件"httr"內的函數GET()設timeout 感謝這位學長! --

03/20 14:32, 3年前 , 8F
或是三樓建議那樣 可能比較好
03/20 14:32, 8F

03/20 14:33, 3年前 , 9F
這段code看起來是我5年前為某老師寫的...
03/20 14:33, 9F

03/20 14:34, 3年前 , 10F

03/20 14:34, 3年前 , 11F
沒想到會在R板看到自己寫的東西XDDD
03/20 14:34, 11F
※ 編輯: CruxLaelaps (124.109.113.253 臺灣), 03/25/2021 15:59:46
文章代碼(AID): #1WL7wyLH (R_Language)