pythonp爬取網頁請求超時。獲取整個響應

未優化:

import requests
import eventlet

data=[]
websites=['http://google.com', 'http://bbc.co.uk']
for w in websites:
    r= requests.get(w, verify=False)

https://eventlet.net/doc/modules/timeout.html

https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings

最後優化:

import requests
import eventlet

data=[]
websites=['http://google.com', 'http://bbc.co.uk']

eventlet.monkey_patch()
for w in websites:
    try:
         with eventlet.Timeout(15):#設置超時時間15s
                r= requests.get(w, verify=False)
                #邏輯代碼
                pass

    except:
        print("響應超時")
        continue #跳出本次循環

print("程序執行完畢")
        

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章