[問題] 爬蟲問題,關於網址不存在

看板Python作者 (牆玫瑰)時間5年前 (2018/10/22 15:44), 編輯推噓4(405)
留言9則, 7人參與, 5年前最新討論串1/1
爬蟲新手請教,程式碼結構如下: import requests url = 'http://www.abc.de.fg' res = requests.get(url) 一般來說,res應該會回傳一個status code 用來判斷server的回應狀態 目前寫的爬蟲,若該網址掛掉 or 不存在, requests.get(url) 會直接噴一樣的 ERROR,造成程式終止 而不會回傳一個適當的status code 想問這是bug 還是 本來就這樣設計? 還是要自己用try except做錯誤處理?? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.130.138.229 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1540194260.A.7DF.html

10/22 17:08, 5年前 , 1F
網址不存在就沒有server,沒有server你哪來 status code?
10/22 17:08, 1F

10/22 17:10, 5年前 , 2F
http status code 是對面 server 給的, 連不到就沒有
10/22 17:10, 2F

10/22 17:48, 5年前 , 3F
Time out ?
10/22 17:48, 3F

10/22 21:12, 5年前 , 4F
try except
10/22 21:12, 4F

10/22 22:15, 5年前 , 5F
try except
10/22 22:15, 5F

10/23 02:19, 5年前 , 6F
沒東西要怎麼回傳,用try-except吧
10/23 02:19, 6F

10/23 06:10, 5年前 , 7F
網路相關的一般都會加 try except
10/23 06:10, 7F

10/23 14:02, 5年前 , 8F

10/23 14:05, 5年前 , 9F
文章代碼(AID): #1RpN_KVV (Python)